I use immutable readonly
fields along with public
Expression<Func<>>
getters all the time as below.
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!
}
I would really like to be able to do that with static methods as above.
I tried different syntax but none worked, ie:
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));
The correct way is as follows
public static Person Named(string surname, string given) => new Person(surname, given);
It's worth mentioning that there is no difference in the syntax for instance and static properties/methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.