C#:匿名类型和属性名称 [英] C#: Anonymous types and property names

查看:379
本文介绍了C#:匿名类型和属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这之间有什么区别吗?

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

和这个:

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

?

推荐答案

它们是相同的;如果未指定名称(并且右侧是简单的成员访问权限),则假定现有成员的名称.该名称仅在以下情况下必需:

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:

  • 将名称更改为 else (例如Name = grp.Key)
  • 为非成员访问表达式命名(例如Count = grp.Count())
  • 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())

这篇关于C#:匿名类型和属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆