【发布时间】:2018-01-02 22:42:15
【问题描述】:
protected override IEnumerable<string> Attributes => new []
{
IntercationAttributes.State
};
谁能解释一下这里发生了什么。为什么我们有带有 lambda 表达式的 new[]。我第一次看到它。
【问题讨论】:
protected override IEnumerable<string> Attributes => new []
{
IntercationAttributes.State
};
谁能解释一下这里发生了什么。为什么我们有带有 lambda 表达式的 new[]。我第一次看到它。
【问题讨论】:
这不是匿名方法(也称为 lambda 表达式),而是一种以更简洁的方式编写计算属性的方法。完全等价于
protected override IEnumerable<string> Attributes {
get { return new [] { IntercationAttributes.State };
}
如您所见,表单的只读属性
<property> { get { return <expression>; } }
可以写成
<property> => <expression>;
在 C# 6 中,稍微减少了声明周围的语法。
【讨论】: