Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding items to the List at creation time in VB.Net

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

like image 874
Shaddix Avatar asked Apr 13 '10 08:04

Shaddix


People also ask

How do I add an item to a list in Visual Basic?

To add items to a ListBox, select the ListBox control and get to the properties window, for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.

What is a list in Visual Basic?

In visual basic, List is a generic type of collection so it will allow storing only strongly typed objects i.e. elements of same data type and the size of list will vary dynamically based on our application requirements like adding or removing elements from the list.


1 Answers

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.

like image 89
Jon Skeet Avatar answered Oct 12 '22 09:10

Jon Skeet