Nell'esempio List<T>
è una definizione di tipo generico. T
è chiamato parametro di tipo generico. Quando il parametro type è specificato come in List<string>
o List<int>
o List<double>
, si ha un tipo generico. Si può vedere che con l'uso di alcuni codice come questo ...
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);
}
che stamperà
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.
Un altro modo per ottenere la definizione di tipo generico direttamente è typeof(List<>)
o typeof(Dictionary<,>)
.
fonte
2010-04-02 02:47:31
Ahhhh e tutto ha senso ora – Micah
Grande spiegazione. – wonde