mir*_*lav 4 c# vb.net types initialization
我对.NET类型系统的高级功能没有经验.我找不到zz的类型(可以写什么而不是var.或者var是最好的选择吗?)
string foo = "Bar";
int cool = 2;
var zz = new { foo, cool }; // Watches show this is Anonymous Type
Run Code Online (Sandbox Code Playgroud)
但最重要的是,如何在VB.NET代码中实现等效(这是我实际需要的).
Dim Foo As String = "Bar"
Dim Cool As Integer = 2
Dim zz = {Foo, Cool} 'This is not an equivalent of above, I'm getting an array
Run Code Online (Sandbox Code Playgroud)
我搜索了几个C#源代码,使用了代码监视器并了解了zz在内部的运行情况,但我无法进行下一步.
(我努力的目的是像这样在VB.NET).
您的代码甚至不会使用OPTION STRICT
设置为ON进行编译,强烈建议使用.没有类型可以从String
+ 派生Int32
.如果你想要一个它会编译Object()
:
Dim zz As Object() = {Foo, Cool}
Run Code Online (Sandbox Code Playgroud)
但是你想创建一个匿名类型,使用New With
:
Dim zz = New With {Foo, Cool}
Run Code Online (Sandbox Code Playgroud)
http://msdn.microsoft.com/en-us/library/bb385125.aspx
使用.NET 4.7和Visual Basic 2017,您还可以使用ValueTuples
名称:
Dim zz = (FooName:=foo, CoolName:=cool)
Run Code Online (Sandbox Code Playgroud)
现在您可以按名称访问元组项:
int cool = zz.CoolName;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3490 次 |
最近记录: |