我写了这个 - 非常简单 - 函数,然后想知道VB有一些预先构建的功能来做到这一点,但找不到任何具体的东西.
Private Shared Function MakeArray(Of T)(ByVal ParamArray args() As T) As T() Return args End Function
没有那么多像被使用
Dim someNames() as string = MakeArray("Hans", "Luke", "Lia")
因为这可以用来完成
Dim someNames() as string = {"Hans", "Luke", "Lia"}
但更像是
public sub PrintNames(names() as string) // print each name End Sub PrintNames(MakeArray("Hans", "Luke", "Lia"))
有任何想法吗?
任何理由不这样做:
Dim someNames() as string = New String()("Han", "Luke", "Leia")
唯一的区别是类型推断,据我所知.
我刚检查过,VB 9也隐式输入了数组:
Dim someNames() as string = { "Han", "Luke", "Leia" }
(据我所知,这在VB 8中不起作用,但显式版本会出现.隐式版本对于匿名类型是必需的,这对于VB 9来说也是新的.)
Dim somenames() As String = {"hello", "world"}