Introducing C# 10: Mix declarations and expressions in a deconstruction
Reminder on deconstruction in C#
Deconstructing an object allows you to assign its properties to variables with the simplest syntax possible. To do this, you obviously have to implement a deconstructor. Let’s take the example of the Person class containing the FirstName and LastName properties, the deconstructor (public void Deconstruct) will look like this:
The two deconstruction syntaxes look like this:
Deconstruction in C# 10
C# 10 allow to mix these syntaxes, like this:
Prior C# 10 you’ll get this compilation error: Error CS8184 A deconstruction cannot mix declarations and expressions on the left-hand-side.
That’s all folks ! 🙂