All the compiler knows about this placeholder type is that it is some kind of object. So you can access all of the object methods, such as Equals and ToString. You can assign them to variables, pass them as parameters, and return them from methods.
But the compiler doesn't know whether it is a value type or a reference type, and whether it has a default constructor. So, in C#, you can't just assign null to it or create one with "new". Instead, you have to use "default". For reference types like classes, interfaces, and delegates, this means the same thing as "null". But for value types like integers, enumerations, and structures, this gives the default value.
In VB, the story is simpler. You can actually assign Nothing to a placeholder. If it represents a value type, then Nothing is replaced with the default value.
Public Class MyGenericClass(Of T)
Private _member As T
Public Function Method() As T
Return Nothing
End Function
End Class
Dim l As Integer
l = New MyGenericClass(Of Integer)() _
.Method
l = New MyGenericClass(Of String)() _
.Method.Length
class MyGenericClass<T>
{
private T _member;
public T Method()
{
return default(T);
}
}
int l;
l = new MyGenericClass<int>()
.Method();
l = new MyGenericClass<String>()
.Method().Length;