Re: generic list constructor
- From: "Andy Fish" <ajfish@xxxxxxxxxxxxxxxx>
- Date: Mon, 24 Nov 2008 19:05:44 -0000
"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
.
- Follow-Ups:
- Re: generic list constructor
- From: Peter Duniho
- Re: generic list constructor
- References:
- generic list constructor
- From: Andy Fish
- Re: generic list constructor
- From: Ignacio Machin ( .NET/ C# MVP )
- generic list constructor
- Prev by Date: GPGraphic.h C# conversion
- Next by Date: Re: GPGraphic.h C# conversion
- Previous by thread: Re: generic list constructor
- Next by thread: Re: generic list constructor
- Index(es):
Relevant Pages
|