我试图从函数返回一个字符串数组,但对一个错误感到惊讶。
我本希望这能起作用,但它会产生错误:
Public Function TestError() As String()
Return {"foo", "bar"}
End Function
这有效:
Public Function TestOK() As String()
Dim ar As String() = {"foo", "bar"}
Return ar
End Function
正如:
Public Function TestOK() As String()
Return New String() {"foo", "bar"}
End Function
我想我不清楚 {} 的含义 - 有没有办法隐式返回字符串数组而不显式创建和初始化它?
最佳答案
Visual Basic 10 与 Visual Basic 9 中对类型推断的数组初始值设定项支持已更改。
在以前版本的 VB 中,需要放置空括号来表示数组。此外,除非另有说明,否则它将把数组定义为对象数组:
' Integer array
Dim i as Integer() = {1, 2, 3, 4}
' Object array
Dim o() = {1, 2, 3}
查看更多信息:
关于vb.net - 在 VB.NET 中声明和初始化字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5262577/