Search This Blog

Thursday, December 29, 2011

what is generics and why it is required ???

The generics are used to assign the data type at run time.


namespace Generics
{
    class Program
    {
        static void Main(string[] args)
        {
            Test<int> t1=new Test<int>();
            t1.Temp = 100;
            Console.WriteLine(t1.Temp);
            Test<string> t2 = new Test<string>();
            t2.Temp = "string";
            Console.WriteLine(t2.Temp);
            Test1<int, string> t = new Test1<int, string>(1028, "This is string");
            int i=t.method1();
            string s=t.method2();
            Console.WriteLine(" "+i+" "+s);
            Console.ReadLine();

        }
    }
    class Test<T>
    {
        T temp;

        public T Temp
        {
            get { return temp; }
            set { temp = value; }
        }


    }

    class Test1<T, TT>
    {
        T temp;
        TT temp1;
        public Test1(T t1,TT tt1)
        {
            temp=t1;
            temp1=tt1;
        }
        public T method1()
        {
            return temp;
        }
        public TT method2()
        {
            return temp1;
        }
    }
}






No comments:

Post a Comment