【发布时间】:2010-10-24 10:39:02
【问题描述】:
C# 的 default(T) - default operator 的 VB 等价物是什么
【问题讨论】:
标签: vb.net default-value
C# 的 default(T) - default operator 的 VB 等价物是什么
【问题讨论】:
标签: vb.net default-value
这些都是:
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 约束时,后者才有可能。
【讨论】:
Debug.Assert 调用。只需抓住并按 F5 即可运行。
与default(T) 最接近的等价物实际上是CType(Nothing, T),因为它可以在使用default(T) 的任何上下文中使用(即作为表达式)。
【讨论】:
If Not id = CType(Nothing, TId) Then...的情况
If Not id Is Nothing Then ...。在这种情况下,您不需要强制转换 Nothing 值。