0

在某些语言中,将项目添加到集合中时会有捷径。

我有以下内容:

Sub Main()

    Dim letters As List(Of String)
    letters = New List(Of String)

    letters.Add("a")
    letters.Add("1")
    letters.Add("2")
    letters.Add("3")
    letters.Add("x")
    letters.Add("d")

End Sub

是否有执行此操作的简便方法。以下不起作用,但可能有一些类似的语法VB.NET

Sub Main()

    Dim letters As List(Of String)
    letters = New List(Of String){"a","1","2","3","x","d"}

End Sub
4

2 回答 2

1

从 VS2010 开始,您应该能够在 VB.NET 中执行此操作:

 Dim letters As List(Of String)
 letters = New List(Of String) From {"a","1","2","3","x","d"}

From可以换成括号:

 letters = New List(Of String) ({"a","1","2","3","x","d"})

不错的相关文章:MSDN HERE

于 2013-01-02T08:37:31.293 回答
1

您可以使用字符串数组(在我的示例中为 input())。另一种选择是将字符串列表传递给构造函数或使用List.AddRange

    Dim input() As String = { "A", _
                              "B", _
                              "C" }

    Dim listA As New List(Of String)(input)

    dim listB as New List(Of String)
    listB.AddRange(input)

HTH韦德

于 2013-01-02T08:49:07.213 回答