Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize ArrayList WITH elements [duplicate]

Possible Duplicate:
Short way to create arrays?

I can create an ArrayList, but, is it possible to create it WITH some elements already? Normally, your arrays are empty, but what if I want to create an array with a few elements already?

like image 925
Voldemort Avatar asked Dec 06 '22 23:12

Voldemort


2 Answers

In VB.NET 2010, you can do things like:

Dim list As New List(Of String) From { "one", "two", "three" }

In 2008 and below you're stuck with initializing your lists after you've instantiated them.

Dim list As New List(Of String)
list.Add("one")
list.Add("two")
list.Add("three")

Or you could shorten it up a bit and do this (this won't work if you declare your List(Of T) as an IList(Of T)):

Dim list As New List(Of String)
list.AddRange(New String() { "one", "two", "three" })
like image 98
Cᴏʀʏ Avatar answered Dec 11 '22 10:12

Cᴏʀʏ


Visual Studio 2010 is out. This works:

Dim list as List(Of String) = 
    New List(Of String)(New String() {"one", "two", "three"})
like image 23
Jasin Avatar answered Dec 11 '22 09:12

Jasin