Use Tuple values in Object initalizer syntax

Viewed 36

I'm trying to initialize properties of an object I am creating with the values of a named tuple.

Something like this

public Person DoIt() {
  return new Person {
    (First, Last) = GetFirstAndLast(id)
  };
}

public (string first, string last) GetFirstAndLast(int id) {
  return ("First name", "Last name");
}

I know I can achieve the same effect by doing this, but I don't want to use an extra variable.

public Person DoIt()
{
    var (first, last) = GetFirstAndLast(0);
    return new Person
    {
        First = first,
        Last = last
    };
}

0 Answers
Related