Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net C#和#x27的VB等效物;s默认值(T)_Vb.net_Default Value - Fatal编程技术网

Vb.net C#和#x27的VB等效物;s默认值(T)

Vb.net C#和#x27的VB等效物;s默认值(T),vb.net,default-value,Vb.net,Default Value,VB与C#的默认值(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中,即使给值类型赋值,也不必赋值。后者只有在为泛型类型指定New或Structure约束时才可能出现。与default(T)最接近的等价物实际上

VB与C#的默认值(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中,即使给值类型赋值,也不必赋值。后者只有在为泛型类型指定
New
Structure
约束时才可能出现。

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

Reflector建议使用以下(但等效)行:Dim变量为T=CType(Nothing,T)。如果有人希望确认此处的版本,我将其拼凑在一起以确认正确版本的结果相同。这是一个精简的控制台应用程序,包含大量
调试.Assert
调用。只需抓取和F5即可运行。不适用于这种情况
如果不是id=CType(Nothing,TId),那么…
这就是值相等。我相信你想要
,如果不是id,那么…
。在此上下文中,您不需要强制转换Nothing值。MyStructInstance.Equals(ctype(Nothing,TypeOfMyStruct))有效。“=”不