C# | Deconstructors with Tuples
Last Updated :
28 Aug, 2019
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.
Example:
CSharp
// C# program to illustrate the concept
// of deconstruction with the tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class GFG {
// This method returns the Pet details
public(string, string, int, int, string) PetDetails(string type,
string name, int height, int age, string color)
{
string p_type = type;
string p_name = name;
int p_height = height;
int p_age = age;
string p_color = color;
return (p_type, p_name, p_height, p_height, p_color);
}
// Main method
static void Main(string[] args)
{
// Creating object of GFG class
GFG g = new GFG();
// Deconstruct the given tuple
// So that we can directly access individual fields
// By explicitly declaring types
(string type, string name, int height, int age, string color) = g.PetDetails("Dog",
"Dollar", 124, 3, "White");
Console.WriteLine("Pet Details:");
Console.WriteLine("Type: " + type);
Console.WriteLine("Name: " + name);
Console.WriteLine("Height: " + height);
Console.WriteLine("Age: " + age);
Console.WriteLine("Color: " + color);
Console.ReadLine();
}
}
}
Output:
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.
Example:
CSharp
// C# program to illustrate the concept
// of deconstruction with the tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class GFG {
// This method returns the Pet details
public(string, string, int, int, string) PetDetails(string type,
string name, int height, int age, string color)
{
string p_type = type;
string p_name = name;
int p_height = height;
int p_age = age;
string p_color = color;
return (p_type, p_name, p_height, p_height, p_color);
}
// Main method
static void Main(string[] args)
{
// Creating object of GFG class
GFG g = new GFG();
// Deconstruct the given tuple
// So that we can directly
// access individual fields
// Using var keyword
var(type1, name1, height1, age1, color1) = g.PetDetails("Dog",
"Dollar", 124, 3, "White");
Console.WriteLine("Pet Details:");
Console.WriteLine("Type: " + type1);
Console.WriteLine("Name: " + name1);
Console.WriteLine("Height: " + height1);
Console.WriteLine("Age: " + age1);
Console.WriteLine("Color: " + color1);
(var type2, var name2, var height2, var age2, var color2) = g.PetDetails("Cat",
"Poo", 104, 1, "Black&White");
Console.WriteLine("\nPet Details:");
Console.WriteLine("Type: " + type2);
Console.WriteLine("Name: " + name2);
Console.WriteLine("Height: " + height2);
Console.WriteLine("Age: " + age2);
Console.WriteLine("Color: " + color2);
Console.ReadLine();
}
}
}
Output:
3. You can deconstruct a tuple into a variable that are already declared. As shown in the below example:
Example:
CSharp
// C# program to illustrate the concept
// of deconstruction with the tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class GFG {
// This method returns the Pet details
public(string, string, int, int, string) PetDetails(string type,
string name, int height, int age, string color)
{
string p_type = type;
string p_name = name;
int p_height = height;
int p_age = age;
string p_color = color;
return (p_type, p_name, p_height, p_height, p_color);
}
// Main method
static void Main(string[] args)
{
// Creating object of GFG class
GFG g = new GFG();
// Declaring and initializing variables
string type = "Cow";
string name = "BooBoo";
int height = 234;
int age = 4;
string color = "Black&white";
// Deconstruct the given tuple
// So that we can directly
// access individual fields
// By declaring variables
(type, name, height, age, color) = g.PetDetails("Cat",
"Mew", 105, 2, "Brown");
Console.WriteLine("Pet Details:");
Console.WriteLine("Type: " + type);
Console.WriteLine("Name: " + name);
Console.WriteLine("Height: " + height);
Console.WriteLine("Age: " + age);
Console.WriteLine("Color: " + color);
Console.ReadLine();
}
}
}
Output:
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, _.
Syntax:
(var1, _, var3, _, var5) = method_name(var1_values, var2_values, var3_value, var4_values, var5_value);
Example:
CSharp
// C# program to illustrate the concept
// of deconstruction with tuple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1 {
class GFG {
// This method returns
// the Pet details
public(string, string, int, int, string) PetDetails(string type,
string name, int height, int age, string color)
{
string p_type = type;
string p_name = name;
int p_height = height;
int p_age = age;
string p_color = color;
return (p_type, p_name, p_height, p_height, p_color);
}
// Main method
static void Main(string[] args)
{
// Creating object of GFG class
GFG g = new GFG();
// Discarding field values
// in deconstruction
(string type, _, int height, _, string color) = g.PetDetails("Dog",
"Dollar", 124, 3, "White");
Console.WriteLine("Pet Details:");
Console.WriteLine("Type: " + type);
Console.WriteLine("Height: " + height);
Console.WriteLine("Color: " + color);
Console.ReadLine();
}
}
}
Output:

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.
Similar Reads
C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her
15+ min read
C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used
15+ min read
C# .NET Framework (Basic Architecture and Component Stack) C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Data Types Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read