Saturday, April 23, 2011

Exception Handling IndexOutOfRangeException

Exception handling for index outof range exception in c sharp.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ass_45
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] name = new int[3];
            Console.WriteLine("Enter Array Value:");
            try
            {
                for (int i = 0; i <= 3; i++)
                {

                    name[i] = Convert.ToInt32(Console.ReadLine());

                }
                for (int i = 0; i <= 3; i++)
                {
                    Console.WriteLine("Array" + name[i]);
                }
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Array Out of Index Ha Ha Ha");
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}

Generics example in c sharp

Generics is belongs from System.Collections.Generic class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generics
{
    public class Test
    {
        public T EmpID
        {
            set;
            get;
        }

        public void show(T t,N n)
        {
            if (typeof(T) == typeof(int) && typeof(N) == typeof(int))
            {
                int x = Convert.ToInt32(t);
                int y = Convert.ToInt32(n);
                Console.WriteLine(x + y);
            }
            else if (typeof(T) == typeof(string) && typeof(N) == typeof(int))
            {
                Console.WriteLine("The " + t + "'s Age is " + n);
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test ob = new Test();
            ob.EmpID = 10;
            ob.show(10, 20);

            Test ob1 = new Test();                        
            ob1.show("Rajesh",20);
        }
    }
}
ASPdotNET-Example