CSharp - typeof Unbound Generic Types


You can get the type of an unbound generic type using typeof operator:

class A<T> {}
class A<T1,T2> {}

Type a1 = typeof (A<>);   // Unbound type (notice no type arguments).
Type a2 = typeof (A<,>);  // Use commas to indicate multiple type args.

The type of an unbound generic type are used in the Reflection API.

You can use the typeof operator to specify a closed type:

Type a3 = typeof (A<int,int>);

or an open type (which is closed at runtime):

class GenericClass<T> { 
   void MyMethod() { 
      Type t = typeof (T); //check the type of your filler type
      Console.WriteLine("you are using "+ t);


using System;
class A<T> {
class A<T1,T2> {
class MainClass{//from  ww  w  .  j a va 2s  .c  om
   public static void Main(string[] args)   {
        Type a1 = typeof (A<>);  
        Type a2 = typeof (A<,>);