Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a generic type and a generic type definition?

Tags:

I'm Studying up on .net reflection and am having a hard time figuring out the difference.

From what I understand, List<T> is a generic type definition. Does that mean that to .net reflection T is the generic type?

Specifically, I guess I'm looking for more background on the Type.IsGenericType and Type.IsGenericTypeDefinition functions.

Thanks!

like image 461
Micah Avatar asked Apr 02 '10 02:04

Micah


People also ask

Is generic type is generic type definition?

IsGenericType tells you that this instance of System. Type represents a generic type with all its type parameters specified. For example, List<int> is a generic type. IsGenericTypeDefinition , on the other hand, tells you that this instance of System.

How does a generic method differ from a generic type?

From the point of view of reflection, the difference between a generic type and an ordinary type is that a generic type has associated with it a set of type parameters (if it is a generic type definition) or type arguments (if it is a constructed type). A generic method differs from an ordinary method in the same way.

How do you define a generic type in TypeScript?

Assigning Generic ParametersBy passing in the type with the <number> code, you are explicitly letting TypeScript know that you want the generic type parameter T of the identity function to be of type number . This will enforce the number type as the argument and the return value.


2 Answers

In your example List<T> is a generic type definition. T is called a generic type parameter. When the type parameter is specified like in List<string> or List<int> or List<double> then you have a generic type. You can see that by running some code like this...

public static void Main() {     var l = new List<string>();     PrintTypeInformation(l.GetType());     PrintTypeInformation(l.GetType().GetGenericTypeDefinition()); }  public static void PrintTypeInformation(Type t) {     Console.WriteLine(t);     Console.WriteLine(t.IsGenericType);     Console.WriteLine(t.IsGenericTypeDefinition);     } 

Which will print

System.Collections.Generic.List`1[System.String] //The Generic Type. True //This is a generic type. False //But it isn't a generic type definition because the type parameter is specified System.Collections.Generic.List`1[T] //The Generic Type definition. True //This is a generic type too.                                True //And it's also a generic type definition. 

Another way to get the generic type definition directly is typeof(List<>) or typeof(Dictionary<,>).

like image 89
Jason Punyon Avatar answered Oct 27 '22 20:10

Jason Punyon


This will help explain it maybe:

List<string> lstString = new List<string>(); List<int> lstInt = new List<int>();  if (lstString.GetType().GetGenericTypeDefinition() ==     lstInt.GetType().GetGenericTypeDefinition()) {     Console.WriteLine("Same type definition."); }  if (lstString.GetType() == lstInt.GetType()) {     Console.WriteLine("Same type."); } 

If you run it you will get the first test to pass because both items are implementing the type List<T>. The second test fails because List<string> is not the same as List<int>. The generic type definition is comparing the original generic before T is defined.

The IsGenericType type is just checking if the generic T has been defined. IsGenericTypeDefinition checks to see that the generic T has NOT been defined. This is useful if you want to know if two objects have been defined from the same base generic type such as the first List<T> example.

like image 34
Kelsey Avatar answered Oct 27 '22 18:10

Kelsey