LINQ - Speed penalty/anonymous types

LINQ - Speed penalty/anonymous types

am 31.03.2008 14:00:44 von PeterLarsen

Hi,

I'm wondering if the use of anonymous types slow down the app at runtime or
not.

See the following sample:

var obj1 = from item in list select new { item.ClassID, AnyName
= item.ClassID };
foreach (var item in obj1)
{
string s1 = item.ClassID;
string s2 = item.AnyName;
}

I know lot of the work is done at compile time, but i still wondering if it
slows down the application to use anonymous types:
Is it slower to use ?

BR
Peter

Re: LINQ - Speed penalty/anonymous types

am 31.03.2008 14:12:31 von skeet

Peter Larsen [CPH] wrote:
> I'm wondering if the use of anonymous types slow down the app at runtime or
> not.
>
> See the following sample:
>
> var obj1 = from item in list select new { item.ClassID, AnyName
> = item.ClassID };
> foreach (var item in obj1)
> {
> string s1 = item.ClassID;
> string s2 = item.AnyName;
> }
>
> I know lot of the work is done at compile time, but i still wondering if it
> slows down the application to use anonymous types:
> Is it slower to use ?

No. It's anonymous at compile time, but it's a perfectly ordinary type
as far as the CLR is concerned. All you're doing in the above is
calling a constructor and then retrieving properties.

Of course, in the above case you could just go directly to the members
of the list, but I'm assuming in real life that you actually have a
reason to use the anonymous type in the first place.

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk