Yes, You can use more than one type parameters in Generic class.For it you can use the parameters list with comma separated ( if you are new with Generics Read this article Generics in C#) . See the following example:
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { public class GenTwoparam<T, S> { T tempObj1; S tempObj2; public GenTwoparam(T obj1, S obj2) { tempObj1 = obj1; tempObj2 = obj2; } public void ShowTypes() { Console.WriteLine("the type of first parameter is " + typeof(T) + " and value is " + tempObj1); Console.WriteLine("the type of first parameter is " + typeof(S) + " and value is " + tempObj2); } } class Program { static void Main(string[] args) { GenTwoparam<int, string> clsObj = new GenTwoparam<int, string>(15, "Welcome"); clsObj.ShowTypes(); Console.ReadKey(); } } }
The output of the above program will be:
the type of first parameter is System.Int32 and value is 15
the type of first parameter is System.String and value is Welcome
From the above we create a GenTwoParam class that is declated with two parameters T and S:
public class GenTwoparam
and we create anew object of the GenTwoParam in the Class1 by providing the two both types as:
GenTwoparam
on here T refers as int and S refers as string type or we can say T is the subsitute of the System.Int32 and S is the subsititute of the System.string.
We can also create the object of the genTwoParam with different type parameters such as:
GenTwoparam