什么是VB相当于C#的default(T)
这是以下任何一个:
Dim variable As T
Dim variable As T = Nothing
Dim variable As New T()
Dim variable As T = CType(Nothing, T) 'this is suggested by reflector
在VB.NET中将Nothing
事件分配给值类型是完全正确的。如果为泛型类型指定New
或Structure
约束,则后者才有可能。
与default(T)
最接近的等价物实际上是CType(Nothing, T)
,因为它可以在任何使用default(T)
的环境中使用(即作为表达式)。