More on deconstruction in C# 7

We looked at deconstruction using the new ValueTuple type in this post. This short post only shows a couple more bits and pieces related to object deconstruction in C# 7.

Consider the following Rectangle class:

public class Rectangle
{
	public int Width { get; set; }
	public int Height { get; set; }
}

Let’s see if we can collect the Width and Height properties into a tuple:

Rectangle r = new Rectangle { Height = 10, Width = 5 };
var (w, h) = r;
Console.WriteLine(w);
Console.WriteLine(h);

So we want to populate “w” and “h” with the values of the Width and Height properties. The compiler will immediately start complaining with multiple error messages:

Cannot infer the type of implicitly-typed deconstruction variable ‘w’
Cannot infer the type of implicitly-typed deconstruction variable ‘h’
‘Rectangle’ does not contain a definition for ‘Deconstruct’ and no extension method ‘Deconstruct’ accepting a first argument of type ‘Rectangle’ could be found (are you missing a using directive or an assembly reference?)
No suitable Deconstruct instance or extension method was found for type ‘Rectangle’, with 2 out parameters and a void return type

It turns out that the class must implement a function called Deconstruct with “out” parameters for those properties that we want to make available for object deconstruction. Here’s our extended Rectangle class:

public class Rectangle
{
	public int Width { get; set; }
	public int Height { get; set; }

	public void Deconstruct(out int w, out int h)
	{
		w = Width;
		h = Height;
	}
}

This will make the compiler immediately happy.

Another solution is to implement the Deconstruct function as an extension method as suggested by one of the previous compiler error messages.

In order to keep our demo examples clean let’s have another Rectangle class:

public class Rectangle2
{
	public int Width { get; set; }
	public int Height { get; set; }
}

Next we need to write an extension method called Deconstruct as follows:

public static class ExtensionMethods
{
	public static void Deconstruct(this Rectangle2 rectangle, out int w, out int h)
	{
		w = rectangle.Width;
		h = rectangle.Height;
	}
}

Make sure that the extension method can be referenced from the class where the Rectangle2 is instantiated. We can then use the same deconstruction syntax as above:

Rectangle2 r2 = new Rectangle2 { Height = 20, Width = 30 };
var (w2, h2) = r2;
Console.WriteLine(w2);
Console.WriteLine(r2);

What if we don’t need every value from a deconstructed object? E.g. if we only want to use the width but “throw away” the height? The underscore is one possible solution like in the following example:

Rectangle r = new Rectangle { Height = 10, Width = 5 };
var (w, _) = r;

From this point on we cannot even refer to a variable called “_”, it is not created or assigned.

View all various C# language feature related posts here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: