在某些语言中,将项目添加到集合中时会有捷径。
我有以下内容:
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
答案 0 :(得分: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
答案 1 :(得分: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 Wade