Skip to content

Instantly share code, notes, and snippets.

@fatihdgn
Created September 30, 2018 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatihdgn/269e42ee85783fca17734a7d7c3a1a03 to your computer and use it in GitHub Desktop.
Save fatihdgn/269e42ee85783fca17734a7d7c3a1a03 to your computer and use it in GitHub Desktop.
public class Person
{
public Person(string name = null, string surname = null, int age = 0)
{
Name = name;
Surname = surname;
Age = age;
}
public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public void Deconstruct(out string name, out string surname, out int age)
{
name = Name;
surname = Surname;
age = Age;
}
public static implicit operator Person((string name, string surname, int age) x) => new Person(x.name, x.surname, x.age);
}
var person = new Person("John", "Doe", 65);
var (name, surname, age) = person; // This works because we have a "Deconstruct" method in our class.
var t = (name, surname, age); // We are creating a new tuple using existing variables.
Person p2 = t; // This works because we have an implicit operator for the (string, string, int) tuple types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment