A tuple is a data structure which gives you the easiest way to represent a data set which has multiple values that may/may not be related to each other. But if you are trying to retrieve multiple fields or property values from the tuple is more difficult. So, to overcome this problem deconstructor was introduced in C# 7.0. It is used to divide the variable values or tuple into parts and assigns those values to the new variables. It also used with classes and structures. Here we only discuss the working of deconstructor with tuples.
In Tuples, deconstructor is used to divide a tuple into parts and assign these parts individually into new variables. So, you can access an individual field or property value. You can deconstruct a tuple in four different ways:
1. You can deconstruct a tuple simply by explicitly declare the type of each field inside the parentheses. But you are not allowed to specify a specific type outside the parentheses even every field in the tuple is of the same type. If you try to do, then you will get an error.
2. You can deconstruct a tuple by using var keyword so that C# infers the type of each variable. You can use the var keyword in two different ways:
- You are allowed to place the var keyword outside of the parentheses.
- You are allowed to place the var keyword individually inside the parentheses with some or all variables.
3. You can deconstruct a tuple into a variable that are already declared. As shown in the below example:
4. You can also use discards in deconstruction. Discards are write-only variables whose values are meant to be ignored. And discards are designated by using an underscore character (“_”) in an assignment. You are allowed to discard as many values as you want and all are represented by the single discard, _.
(var1, _, var3, _, var5) = method_name(var1_values, var2_values, var3_value, var4_values, var5_value);
Note: In deconstruction, you need to assign each element to a variable if you eliminate any element, then the compiler will give an error. And you are not allowed to mix declarations and assignments to existing variables on the left-hand side of a deconstruction if you eliminate any element, then the compiler will give an error.
- Difference between Python and C#
- Difference between C and C#
- Writing to Excel Sheet Using EPPlus in C#
- Lambda Expressions in C#
- How to Get Started with Game Development?
- Basics of File Handling in C#
- C# | ToolTip Class
- C# | RichTextBox Class
- C# | MaskedTextBox Class
- C# | NumericUpDown Class
- C# | DateTimePicker Class
- C# | GroupBox Class
- C# | ListBox Class
- Different ways to convert String to Integer in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.