Saturday, 3 March 2012

Can we create a Generic method with multiple arguments?


Yes, We can create generic method with more then one type also. And each type is sperated with comma(,).
Ex:
Void Genericmethod<T1, T2>(T1,T2)
{

}
  as well as different arugments also
Genericmethod<int,int>(10,100);
Genericmethod<double,double>(40.3, 25.7);
and different arguments
Genericmethod<int,string>(10,"welcome");
……………ect
Void Genericmethod(T1,T2,T3)
{

}
Calling is
Genericmethod<int,double,string>(10,105.7,”kasi”)
When we calling that method we mast pass those apporiate datatype to called method in same order
Generic method with multiple types(int,float,double..ect) and values.
Ex:
class MultipleGenericMethods
    {
        static void print<mytype1, mytype2>(mytype1 x, mytype2 y)
        {
            Console.WriteLine("x:{0}\tY:{1}",x,y);               
        }
        static void Main()
        {
            print<int,double>(10,45.62);
            print<string,DateTime>("welcome",DateTime.Now);
            print<string,string>("kasi","babu");
            //print<userdefine datatype,userdefine datatype>(employee,job);
            Console.ReadLine();       
        }
 }

No comments:

Post a Comment