Archive for January 2016

C#.NET Collections

Collections are used to store multiple values of object type. Memory for items is allocated dynamically and they can be accessed using indexes.

Collection classes are available under System.Collections namespace.

Types of collections are:

ArrayList

Stack

Queue

HashTable

ArrayList

It is used to store multiple values of object type. It is an alternative for array.

Memory for items inside it is allocated and deallocated dynamically.

Indexes are used to access elements inside it.

Predefined methods of ArrayList are used to perform operations easily on ArrayList.

Stack

It follows a style of Last- In, First - out (LIFO) for inserting or removing elements from Stack Collections.

Memory is allocated dynamically for items in Stack and it accepts items of object type.

Indexes will not work for accessing items in Stack.

Push method is used to add items in Stack and it is known as pushing.

Pop method is used to remove items from Stack and it is known as popping.

Queue

It follows a style of First- In, First - out (FIFO) for inserting or removing elements from Queue Collections.

Memory is allocated dynamically for items in Queue and it accepts items of object type.

Indexes will not work for accessing items in Queue.

Enqueue method is used to add items in Queue.

Dequeue method is used to remove items from Queue.

HashTable

It uses a key to access the elements in the collection.

Elements in HashTable are accessed used key,

Each item in the HashTable has a key/value pair.

Using key the item value is accessed.
Sunday, 10 January 2016
Posted by Sudhir Chekuri
Tag :

C#.NET Exception Handling

Exception

Exceptions are the runtime errors that leads to abnormal termination of program.

Exception handling

It is a mechanism to handle the abnormal termination of program because of runtime errors.
We can handle exceptions using try catch block.
Parent of all exceptions is Exception class.

Try block

To use exception handling you must place the code that you want to guard against an exception in the try block.
try
{

}

Catch block

Code to handle the exceptions has to be written in the catch block.

catch
{

}

Finally block

The block of code that has to be executed even exception occurs or not.

finally
{

}

Throw block

Used to force an exception.
when an exception is raised in try block it is throwed to catch block to handle it.
exceptions are throwed automatically by the compiler.
user defined throws can also be used to throw an exception.

Simple try catch block example program

Program
using System;
namespace exception
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, z;
            try
            {
                x = 5;
                y = 0;
                z = x / y;
                Console.WriteLine(z);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

Example program for displaying userdefined message in catch block

Program
using System;
namespace exception
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y;
            try
            {
                x = 5;
                y = 0;
                if (y < 1)
                {
                    throw new ApplicationException("error");
                    x = x / y;
                } Console.WriteLine(x);
            }

            catch (Exception ex)
            {
                Console.WriteLine("this is an error message");

            }
            finally
            {
                Console.WriteLine("definetly executed");
                Console.ReadKey();
            }
        }
    }
}

Example program using dividebyzeroexception class to handle exception

Program
using System;
namespace exception
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, z;
            try
            {
                x = 5;
                y = 0;
                z = x / y;
                Console.WriteLine(z);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

The above catch block can handle only dividebyzero exception if another exception is raised program is again terminated abnormally.

Example program to use Multiple catch blocks

Program
using System;
namespace exception
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, z;
            try
            {
                x = 5;
                y = 0;
                z = x / y;
                Console.WriteLine(z);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
}

Example program using try, catch and finally block

Program
using System;
namespace exception
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, z;
            try
            {
                x = 5;
                y = 0;
                z = x / y;
                Console.WriteLine(z);
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine(ex.Message);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            finally
            {
                Console.WriteLine("definetly executed");
                Console.ReadKey();
            }
        }
    }
}

Example C# program for user defined throw

Program
using System;
namespace exception
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y;
            try
            {
                x = 5;
                y = 0;
                if (y < 1)
                {
                    throw new ApplicationException("error");
                    x = x / y;
                } Console.WriteLine(x);
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            finally
            {
                Console.WriteLine("definetly executed");
                Console.ReadKey();
            }
        }
    }

}
Saturday, 9 January 2016
Posted by Sudhir Chekuri
Tag :

C#.NET Delegate

  • Delegate is a type which can hold reference of multiple methods.
  • All the methods should have same signature of the delegate.
  • It contains return type.
  • The i/o parameters of the delegate should be exactly same as i/o parameters of the method.
  • Semicolon is required at the end of delegate.
Syntax

public delegate void/type delegatename(parameters);

Delegates are of 2 types.

1. Singlecast delegate

2. Multicast delegate

Singlecast Delegate

If a delegate is used for invoking a single method it is called as "Singlecast delegates".

Example for Singlecast delegate
using System;

namespace delegates
{
    class Program
    {
        static void Main(string[] args)
        {
            del d = new del(Program.add);
            int i = d(12, 44);
            Console.WriteLine(i);
            Console.ReadKey();
        }
        public static int add(int c, int d)
        {
            return c + d;
        }
    }
    public delegate int del(int a, int b);
}



Output

56

Multicast Delegate

If a delegate is used for calling more than one method it is called as "Multicast delegates". In multicasting delegates method should have only void type. It can hold reference of multiple methods.

Example for multicast delegate

Program
using System;

namespace delegates
{
    class delegat
    {
        static void Main(string[] args)
        {
            del d = new del(delegat.add);
            d = d + delegat.sub;
            d = d + delegat.mul;
            d = d + delegat.div;
            d(12, 44);
            Console.ReadKey();
        }
        public static void add(int c, int d)
        {
            Console.WriteLine(c + d);
        }
        public static void sub(int c, int d)
        {
            Console.WriteLine(c - d);
        }
        public static void mul(int c, int d)
        {
            Console.WriteLine(c * d);
        }
        public static void div(int c, int d)
        {
            Console.WriteLine(c / d);
        }
    }
    public delegate void del(int a, int b);
}

Output

56

-32

528

0
Posted by Sudhir Chekuri
Tag :

Abstract class VS Interface

  • Abstract class can contain abstract methods and general methods.
  • Interface can contain only abstract methods. 
  • All the methods in interface by default public and abstract.
Posted by Sudhir Chekuri
Tag :

C#.NET Interface

  • Interface is a collection of "abstract methods".
  • All the methods in interface will be abstract methods.
  • A class inheriting interface should implement abstract methods present in that interface.
  • Multiple inheritance is possible in interfaces.
  • Body of the abstract method should be given in the inherited classes.
  • All the methods in interface are by default Public and Abstract.
Syntax

interface interfacename

{

body

}

Example Program using interface
using System;

namespace interfaces
{
    class Program : abc
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.add();
            Console.ReadKey();
        }
        //implementing method in interface
        public void add()
        {
            Console.WriteLine("add method");
        }
    }
    interface abc
    {
        //by default public and abstract
        void add();
    }
}

Output

add method

Inheriting Multiple interfaces

  • It is one type of interface.
  • It consists of multiple methods and multiple interfaces.
  • To inherit multiple interfaces,use inheritance concept.
Example program for multiple inheritance using interfaces

Program
using System;

namespace interfaces
{
    class Program : abc, def
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.add();
            p.sub();
            Console.ReadKey();
        }
        //implementing method in interface
        public void add()
        {
            Console.WriteLine("add method");
        }
        public void sub()
        {
            Console.WriteLine("sub method");
        }
    }
    interface abc
    {
        //by default public and abstract
        void add();
    }
    interface def
    {
        //by default public and abstract
        void sub();
    }
}

Output

add method

sub method

Example for Inheriting Multiple interfaces and implementing both methods with same name in derived class
Program
using System;
namespace Interfaces
{
    class Program
    {
        static void Main(string[] args)
        {
            a x = new c();
            x.Add();//AdditionA
            b y = new c();
            y.Add();//AdditionB
            Console.ReadKey();
        }
    }
    interface a
    {
        void Add();

    }
    interface b
    {
        void Add();

    }
    class c : a, b
    {
        void a.Add()
        {
            Console.WriteLine("AdditionA");
        }

        void b.Add()
        {
            Console.WriteLine("AdditionB");
        }

    }
}
Output
AdditionA
AdditionB
Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -