vb.net 从函数返回一个字符串数组而不先初始化它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1880056/
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
Return a string array from a function without initialising it first
提问by Richard Lucas
Public Function Foo() as String()
Dim bar As String = {"bar1","bar2","bar3"}
Return bar
End Function
My situation is similar to the code sample above where I'm returning a string array from a function.
我的情况类似于上面的代码示例,我从函数返回一个字符串数组。
What I would like to do is just return the string array without having to declare a variable first and then return the variable.
我想要做的只是返回字符串数组,而不必先声明变量然后返回变量。
Something like this, although this obviously doesn't work:
像这样的东西,虽然这显然不起作用:
Return {"bar1","bar2","bar3"}
Is it possible to do this, I can't seem to find a method that works?
是否可以这样做,我似乎找不到有效的方法?
回答by Darin Dimitrov
You could do this:
你可以这样做:
Public Function Foo() As String()
Return New String() {"bar1", "bar2", "bar3"}
End Function
回答by Oded
You do not have to declare a variable (as the example from Darin), but you do have to create an instance of the type you want (a string array).
您不必声明变量(如 Darin 中的示例),但必须创建所需类型的实例(字符串数组)。
His example works because he is "newing up" a string array.
他的例子之所以有效,是因为他正在“更新”一个字符串数组。