How to declare an array inline in VB.NET

后端 未结 6 1755
难免孤独
难免孤独 2021-02-04 23:30

I am looking for the VB.NET equivalent of

var strings = new string[] {\"abc\", \"def\", \"ghi\"};
6条回答
  •  余生分开走
    2021-02-05 00:09

    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#:

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

    Compile it, keeping debug information:

    csc /debug+ Test.cs
    

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

    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.

提交回复
热议问题