Wednesday, April 6, 2011

C#: Anonymous types and property names

Is there any difference at all between this:

dataContext.People.Select(ø => new
{
     Name = ø.Name,
});

and this:

dataContext.People.Select(ø => new
{
     ø.Name,
});

?

From stackoverflow
  • They are identical; if no name is specified (and the right-hand-side is a simple member-access) then the name of the existing member is assumed. The name is only necessary to:

    • change the name to something else (for example Name = grp.Key)
    • to give a name to a non-member-access expression (for example Count = grp.Count())
    Kelly : haha too fast :P
    Svish : on his reputation, it looks like he has some practice :p
  • No. The second simply derives the name of the property for you, the actual code generated is the same.

  • No, the compiler will name the property of the anonymous type the same as the right hand side of the assignment.

0 comments:

Post a Comment