How to do it...

  1. If you created the Student class earlier, you should have something similar to this in your code:
        public class Student        {          public string Name { get; set; }          public string LastName { get; set; }          public List<int> CourseCodes { get; set; }        }
  1. To create a deconstructor, add a Deconstruct method to your Student class. You will notice that this is a void method that takes two out parameters (in this instance). We then just assign the values of Name and LastName to the out parameters.
If we wanted to deconstruct more values in the Student class, we would pass in more out parameters, one for each value we wanted to deconstruct.
        public void Deconstruct(out string name, out string lastName)        {          name = Name;          lastName = LastName; }  ...

Get C# 7 and .NET Core Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.