【问题标题】:using new[] with lambda [duplicate]使用带有 lambda 的 new[] [重复]
【发布时间】:2018-01-02 22:42:15
【问题描述】:
protected override IEnumerable<string> Attributes => new []
{
  IntercationAttributes.State
};

谁能解释一下这里发生了什么。为什么我们有带有 lambda 表达式的 new[]。我第一次看到它。

【问题讨论】:

    标签: c# linq lambda c#-6.0


    【解决方案1】:

    这不是匿名方法(也称为 lambda 表达式),而是一种以更简洁的方式编写计算属性的方法。完全等价于

    protected override IEnumerable<string> Attributes {
      get { return new [] { IntercationAttributes.State };
    }
    

    如您所见,表单的只读属性

    <property> { get { return <expression>; } }
    

    可以写成

    <property> => <expression>;
    

    在 C# 6 中,稍微减少了声明周围的语法。

    【讨论】:

    • 很酷,非常感谢您的解释
    猜你喜欢
    • 2020-12-29
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2017-09-21
    • 2012-08-10
    • 1970-01-01
    相关资源
    最近更新 更多