如何在 VB.NET 中声明一个内联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/291413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:54:53  来源:igfitidea点击:

How to declare an array inline in VB.NET

arraysvb.netliterals

提问by erik

I am looking for the VB.NET equivalent of

我正在寻找相当于的 VB.NET

var strings = new string[] {"abc", "def", "ghi"};

回答by gfrizzle

Dim strings() As String = {"abc", "def", "ghi"}

回答by Jon Skeet

There are plenty of correct answers to this already now, but here's a "teach a guy to fish" version.

现在已经有很多正确的答案,但这里有一个“教一个人钓鱼”的版本。

First create a tiny console app in C#:

首先在 C# 中创建一个小型控制台应用程序:

class Test
{
    static void Main()
    {
        var strings = new string[] {"abc", "def", "ghi"};
    }
}

Compile it, keeping debug information:

编译它,保留调试信息:

csc /debug+ Test.cs

Run Reflectoron it, and open up the Main method - then decompile to VB. You end up with:

在其上运行Reflector,并打开 Main 方法 - 然后反编译为 VB。你最终得到:

Private Shared Sub Main()
    Dim strings As String() = New String() { "abc", "def", "ghi" }
End Sub

So we got to the same answer, but without actually knowing VB. That won't always work, and there are plenty of other conversion tools out there, but it's a good start. Definitely worth trying as a first port of call.

所以我们得到了相同的答案,但实际上并不了解 VB。这并不总是有效,还有很多其他转换工具,但这是一个好的开始。作为第一个停靠港绝对值得一试。

回答by Netricity

In newer versions of VB.NET that support type inferring, this shorter version also works:

在支持类型推断的较新版本的 VB.NET 中,这个较短的版本也适用:

Dim strings = {"abc", "def", "ghi"}

回答by David Mohundro

Dim strings As String() = New String() {"abc", "def", "ghi"}

回答by Jesper Palm

Not a VB guy. But maybe something like this?

不是VB的家伙。但也许是这样的?

Dim strings = New String() {"abc", "def", "ghi"}

(About 25 seconds late...)

(大约迟到 25 秒……)

Tip: http://www.developerfusion.com/tools/convert/csharp-to-vb/

提示:http: //www.developerfusion.com/tools/convert/csharp-to-vb/

回答by Steve Wright

Dim strings As String() = {"abc", "def", "ghi"}

Dim strings As String() = {"abc", "def", "ghi"}