views:

72

answers:

2

In c# I can initialize a List at creation time like

var list = new List<String>() {"string1", "string2"};

is there a similar thing in VB.Net? Currently I can do it like

Dim list As New List(Of String)
list.Add("string1")
list.Add("string2")
list.Add("string3")

but I want to avoid boring .Add lines

+1  A: 

VB10 supports collection initializers. I believe your example would be:

Dim list As New List(Of String) From { "string1", "string2", "string3" }

MSDN has more information.

Jon Skeet
Thank you very much!That's exactly what I need. Unfortunately, it doesn't work in VS2008, so it's another reason to switch to newer VS. But anyway it's the answer: it's impossible in VB prior to VS2010
Shaddix
Not impossible, just ugly.
ho1
A: 

Dim a As New List(Of String)(New String() {"str1", "str2"})

Though if it's VB 2010 I'd definitely go with Jon Skeet's answer.

ho1
thanks! Using "From" is better, but it's also nice workaround
Shaddix