C #: anonymous types and property names

Is there any difference between this:

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

and this:

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

?

+3
source share
3 answers

They are identical; if the name is not specified (and the right side is easy access to the member), then it is assumed that the name of the existing member. Name is required only for:

  • change the name to another (e.g. Name = grp.Key)
  • to name a non-member expression (e.g. Count = grp.Count())
+6
source

No. The second just prints the property name for you, the actual generated code is the same.

+1
source

, , .

+1

Source: https://habr.com/ru/post/1703169/


All Articles