【发布时间】:2015-10-07 07:49:46
【问题描述】:
我一直使用不可变的 readonly 字段和 public Expression<Func<>> getter,如下所示。
public class Person
{
public static Person Named(string surname, string given) { return new Person(surname, given); } // ugly!
protected Person(string surname, string given) { _surname = surname; _given = given; }
private readonly string _surname;
private readonly string _given;
public string Name => _given + _surname; // cool!
}
我真的很希望能够使用上述静态方法来做到这一点。
我尝试了不同的语法,但没有奏效,即:
public static Person Named => x,y => new Person(x,y);
public static Person Named = (x,y) => new Person(x,y);
public static Person Named => ((x,y) => new Person(x,y));
【问题讨论】:
标签: c# lambda delegates expression func