Azurewrath

Hi all,

Anyone knows if I can initialize a List like an array, like this:

string[] s = new string[2] {"a", "b"};

So

List<int> i = new List<int> {1,2,3};







Thanks,
Aw


Re: Visual C# Language List Initialization

IsshouFuuraibou

Part of it is becuase of the way generics and the collection is handled.

The ability to define a set and use it as the initial set of values. Part of it is where the work occurs. with the string[] definition, it can convert { "a", "b" } to s string[] array during compilation. With the use of generics List<T> can't be converted to a premade array because the type is not bound until after compilation to IL.





Re: Visual C# Language List Initialization

Peter Ritchie

Visual C# 2005 and older doesn't have a collection initializer syntax. You can only initialize arrays with values at allocation.

Visual C# 2008 (C# 3.0) includes the collection initializer syntax, where you can initialize a collection similarly to arrays. For example:

Code Block

List list = new List(){1,2,3};

You can also initialize complex types in the collection initializer even if the complex type doesn't necessarily have an appropriate constructor. For example, the following are two ways of initializing a collection of Point structures in C# 3.0:

Code Block
List list = new List{new Point(1,2), new Point(3,4)};

Code Block
List list2 = new List{new Point {X = 1, Y = 2}, new Point { X = 3, Y = 4}};

For the second syntax to work the class must have a publicly available default (parameter-less) constructor and publicly available properties (in this case X and Y).






Re: Visual C# Language List Initialization

Azurewrath

Thanks guys!

Now I have 3 questions:

1. In your first example, if I write:

List list = new List(){1.1,2.2,3.3};

Would this list be of type float or double, or decimal I guess the lowest possible, which is float right

2. When you say it would work, even if there is no appropriate constructor, do you mean this one :

List list = new List{new Point(1,2), new Point(3,4)};

3. Can I initialize complex types like this :

List list = new List{{1,2}, {3,4}};

I guess not, because it would be Point, Size, etc right




Thanks alot,
Aw





Re: Visual C# Language List Initialization

Peter Ritchie

Peter Ritchie wrote:

Visual C# 2005 and older doesn't have a collection initializer syntax. You can only initialize arrays with values at allocation.

Visual C# 2008 (C# 3.0) includes the collection initializer syntax, where you can initialize a collection similarly to arrays. For example:

Code Block

List list = new List(){1,2,3};

You can also initialize complex types in the collection initializer even if the complex type doesn't necessarily have an appropriate constructor. For example, the following are two ways of initializing a collection of Point structures in C# 3.0:

Code Block
List list = new List{new Point(1,2), new Point(3,4)};

Code Block
List list2 = new List{new Point {X = 1, Y = 2}, new Point { X = 3, Y = 4}};

For the second syntax to work the class must have a publicly available default (parameter-less) constructor and publicly available properties (in this case X and Y).

Sorry, the Forums editor mangled my code. It should be:
Code Block

List<int> list = new List<int>(){1,2,3};

Code Block
List<Point> list = new List<Point>{new Point(1,2), new Point(3,4)};

Code Block
List<Point> list2 = new List<Point>{new Point {X = 1, Y = 2}, new Point { X = 3, Y = 4}};





Re: Visual C# Language List Initialization

Azurewrath

Thanks Peter.

Now I have only question:

Can this:

List< int > list = new List< int >(){1,2,3};

be written like this:

List < int > list = {1,2,3};






Thanks again,
Aw




Re: Visual C# Language List Initialization

MarkTheNuke

In .NET 2.0 I would write the initializer like this:

List<int> list = new List<int>(new int[] { 1, 2, 3 });

yes, that means it would generate an array, but it would probably provide a smaller code footprint than adding each item separately.





Re: Visual C# Language List Initialization

Azurewrath

Thanks Mark.

How that code you posted compare to:


List<int> list = new List <int> () {1,2,3};



But I also wonder if we can write this:

List<int> list = {1,2,3};

Because Arrays allow this I think.





Thanks,
Aw




Re: Visual C# Language List Initialization

Sabrecat

Azurewrath wrote:
Thanks guys!

1. In your first example, if I write:

List list = new List(){1.1,2.2,3.3};

Would this list be of type float or double, or decimal I guess the lowest possible, which is float right

I would think that since you have not specified any modifiers that you would get float's. IE if you wanted decimals you would do

List list = new List(){1.1m,2.2m,3.3m);





Re: Visual C# Language List Initialization

Azurewrath

Thanks man, I would still like to be able to use this:

List<int> list = {1,2,3};




Thanks,
Aw