VB等效于C#的默认值(T)

问题描述 投票:44回答:2

什么是VB相当于C#的default(T)

vb.net
2个回答
33
投票

这是以下任何一个:

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事件分配给值类型是完全正确的。如果为泛型类型指定NewStructure约束,则后者才有可能。


43
投票

default(T)最接近的等价物实际上是CType(Nothing, T),因为它可以在任何使用default(T)的环境中使用(即作为表达式)。

© www.soinside.com 2019 - 2024. All rights reserved.