Getting to know about deconstruction syntax
Deconstruction is a syntax to split a value into multiple parts and store those parts individually into new variables. For example, tuples that return multiple values. Let us take the following method as an example, which returns a tuple of two string variables, Title
and Author
(see the New changes to tuples section):
public (string Title, string Author) GetBookDetails() { return (Title: "Mastering Visual Studio 2017", Author: "Kunal Chowdhury"); }
Now, when you call the method GetBookDetails()
, it will return a tuple. You can access its elements by calling the element name, as follows:
var bookDetails = GetBookDetails(); // returns Tuple Console.WriteLine("Title : " + bookDetails.Title); Console.WriteLine("Author : " + bookDetails.Author);
In a deconstructing declaration, you can split the tuple into parts and assign them directly into new variables. You can then access those variables individually...