Skip to content

Instantly share code, notes, and snippets.

@Blizzardo1
Created May 31, 2023 02:06
Show Gist options
  • Save Blizzardo1/c5d3813e358e5e66f72531a3bce2c101 to your computer and use it in GitHub Desktop.
Save Blizzardo1/c5d3813e358e5e66f72531a3bce2c101 to your computer and use it in GitHub Desktop.
internal class Person
{
public string Name { get; }
public int Age { get; }
public string EmailAddress { get; }
/// <summary>
/// Deconstruct Test Class
/// </summary>
/// <param name="name">Name of a Person</param>
/// <param name="age">Age of a Person</param>
/// <param name="emailAddress">Email of a Person</param>
/// <exception cref="ArgumentNullException"><see cref="Name"/> and <see cref="EmailAddress"/> shall not be Null</exception>
public Person(string name, int age, string emailAddress)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Age = age;
EmailAddress = emailAddress ?? throw new ArgumentNullException(nameof(emailAddress));
}
/// <summary>
/// Deconstruct Method
/// </summary>
/// <param name="name">Name of a Person</param>
/// <param name="age">Age of a Person</param>
/// <param name="emailAddress">Email of a Person</param>
public void Deconstruct(out string name, out int age, out string emailAddress) {
name = Name;
age = Age;
emailAddress = EmailAddress;
}
}
internal class TestClass
{
public Person Person { get; }
public TestClass() {
Person = new Person("Adonis Deliannis", 31, "[email protected]");
}
public void PrintPersonDetails()
{
(string name, int age, string emailAddress) = Person;
// Using Deconstruct
Console.WriteLine(@$"Name: {name}, Age: {age}, Email: {emailAddress}");
// Traditional
Console.WriteLine(@$"Name: {Person.Name}, Age: {Person.Age}, Email: {Person.EmailAddress}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment