Re: generic list constructor

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin@xxxxxxxxx> wrote in
message
news:bbb62108-77fb-4359-9626-cfc5ecb2e443@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Nov 24, 11:17 am, "Andy Fish" <ajf...@xxxxxxxxxxxxxxxx> wrote:
Can Someone explain why this snippet doesn't work in C# 2.0:

Foo foo = new Foo();
List<Foo> lf = new List<foo>( { foo } );

whereas this works:

Foo foo = new Foo();
Foo[] af = { foo };
List<Foo> lf = new List<foo>( af );

It seems to me that the expression { foo } returns an array of Foo with
only
one element in it, which should therefore be acceptable as
IEnumerable<Foo>
to be passed into the generic list constructor.

TIA

Andy

Try:
List<Foo> lf = new List<foo>(new Foo[] { foo } );

In 3.5 the initialization of classes, collections were improved a lot,
these improvements do not exist in 2.0 though.

hmm thanks, that works too - although i still don't really understand
exactly why my original example was invalid


.



Relevant Pages