In C# 7, how do I write an Expression Bodied Constructor like this using 2 parameters.
public Person(string name, int age)
{
Name = name;
Age = age;
}
You don't, basically. Expression-bodied members are only available when you have a single statement to execute. You have two in this case.
I mean, you could use tuples, but I strongly advise against it:
// DO NOT USE THIS CODE - IT'S JUST TO DEMONSTRATE THE FEASIBILITY
public class Person
{
private readonly (string name, int age) tuple;
public string Name => tuple.name;
public int Age => tuple.age;
public Person(string name, int age) => tuple = (name, age);
}
That works because you've now only got one statement (tuple = (name, age);
). But you certainly shouldn't change your fields just so that you can use an expression-bodied constructor.
(As David has shown, you can build the tuple then deconstruct it to the read-only auto-props directly, too - that's slightly more pleasant than this, but it's still not worth doing IMO.)
See more on this question at Stackoverflow