C# 7: Deconstructors

With the release of Visual Studio 2017 RC, you can now explore the upcoming C# 7 features. In this article, let’s see how you can use Deconstructors in C# 7.

Please do not confuse Deconstructors with Destructors as Destructors has nothing to with Deconstructors.

Basically, what a Constructor would do is it will create a new object of a given type with given parameters (but opting out the default constructor as it has no parameters). So what the Deconstructor would do is it will deconstruct the object back into its original parts. To be specific, you have the control of specifying how would you like the object to be deconstructed.

Let’s go by an example. Here there is a class named Employee, and there is a Constructor which accepts two parameters, First Name & Last Name, and those are being set to Employee’s properties. (Please note that as of today no errors are thrown by the compiler if you have a return type other than Void in Deconstructor methods. In other words, current Deconstructors lets you return values even though it doesn't make any sense. Microsoft is aware of this and they will get it fixed in the coming releases.)

public class Employee
{
    public string FirstName { get; }
    public string LastName { get; }
 
    public Employee(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = LastName;
    }
}

Above code is pretty simple. And now let’s see how we can add a Deconstructor to Employee.

public class Employee
{
    public string FirstName { get; }
    public string LastName { get; }
 
    public Employee(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = LastName;
    }
 
    public void Deconstruct(out string firstName, out string lastName)
    {
        firstName = FirstName;
        lastName = LastName;
    }
}

It’s simple, isn’t it? The only thing we need to have is, we should have a public void method named Deconstruct and one or more property that we want the object to be deconstructed into, should be specified as out parameters.

Now let’s see how we can call the Deconstructor.

Employee employee = new Employee("Jaliya", "Udagedara");
var (firstName, lastName) = employee;
 
Console.WriteLine(firstName);
Console.WriteLine(lastName);

Something to note is Deconstructor is being invoked by C# 7 Tuple syntax. If you are not aware of Tuples in C# 7, you can read this article.

And the following will be the output.

https://lh3.googleusercontent.com/-3bm7cYuHGaA/WDfFNyKg14I/AAAAAAAAERg/9ygBThJzalI/image_thumb%25255B1%25255D.png?imgmax=800
Output

And the nice thing is, you can have multiple Deconstructors with different parameters (that’s basically method overloading).

public void Deconstruct(out string firstName, out string lastName)
{
    firstName = FirstName;
    lastName = LastName;
}
 
public void Deconstruct(out string firstName)
{
    firstName = FirstName;
}

The respective Deconstructor invocations are as follows.

// first deconstructor invocation
var (firstName, lastName) = employee;
 
// second deconstructor invocation
var (firstName1) = employee; 

If you still couldn’t download Visual Studio 2017 RC, download and try out these new C# 7 features.

Happy Coding.