- Mastering Visual Studio 2017
- Kunal Chowdhury
- 521字
- 2021-07-15 17:26:39
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, just like a local variable:
(string title, string author) = GetBookDetails(); Console.WriteLine("Title : " + title); Console.WriteLine("Author : " + author);
While deconstructing, you can also use var for the individual variables, instead of strong type names:
(var title, var author) = GetBookDetails(); Console.WriteLine("Title : " + title); Console.WriteLine("Author : " + author);
The preceding code can also be replaced by the following deconstruction syntax, by putting a single var outside the parentheses as an abbreviation:
var (title, author) = GetBookDetails(); Console.WriteLine("Title : " + title); Console.WriteLine("Author : " + author);
If you have existing variables with a deconstructing assignment, you can use the same variables to store the return values of a new call:
var (title, author) = GetBookDetails(); // deconstruct to existing variables (title, author) = GetAnotherBookDetails(); Console.WriteLine("Title : " + title); Console.WriteLine("Author : " + author);
Until now, we have learned how to write the syntax to deconstruct a tuple. In C# 7.0, it's not limited to tuples only. You can use deconstructing assignments for any type, as long as it has a Deconstruct method in the following form: public void Deconstruct (out T1 x1, ..., out Tn xn) { ... }.
For example, let us create a class named Book exposing the Deconstruct method with two out variables:
public class Book { public void Deconstruct (out string Title, out string Author) { Title = "Mastering Visual Studio 2017"; Author = "Kunal Chowdhury"; } }
As we didn't expose any properties to access the values of Title and Author from the Book class, we will not find them generally in the IntelliSense popup when trying to access an instance of the class. Refer to the following screenshot:

As we have declared the Deconstruct method in the class, you can directly deconstruct the instance into multiple variables to get the desired output of the return values:
var book = new Book(); var (title, author) = book; //var (title, author) = new Book(); Console.WriteLine("Title : " + title); Console.WriteLine("Author : " + author);
When you run the preceding code, you will get the following output in the console window:

- Node.js 10實(shí)戰(zhàn)
- Android和PHP開(kāi)發(fā)最佳實(shí)踐(第2版)
- C語(yǔ)言程序設(shè)計(jì)案例教程(第2版)
- Java編程指南:基礎(chǔ)知識(shí)、類庫(kù)應(yīng)用及案例設(shè)計(jì)
- 程序員考試案例梳理、真題透解與強(qiáng)化訓(xùn)練
- Drupal 8 Module Development
- 精通Linux(第2版)
- Python忍者秘籍
- Julia高性能科學(xué)計(jì)算(第2版)
- SQL Server數(shù)據(jù)庫(kù)管理與開(kāi)發(fā)兵書(shū)
- Python機(jī)器學(xué)習(xí):預(yù)測(cè)分析核心算法
- Developing SSRS Reports for Dynamics AX
- 數(shù)據(jù)結(jié)構(gòu):Python語(yǔ)言描述
- After Effects CC案例設(shè)計(jì)與經(jīng)典插件(視頻教學(xué)版)
- Python程序設(shè)計(jì)現(xiàn)代方法