Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are Deconstructors in C# 7.0?
Deconstructors in C# 7.0 are methods that allow you to extract multiple values from an object into separate variables in a single assignment. They enable tuple-like deconstruction of custom types, making it easier to work with objects that contain multiple related values.
A deconstructor is defined using the Deconstruct method name with out parameters. When you use tuple syntax on the left side of an assignment, C# automatically calls the appropriate Deconstruct method.
Syntax
Following is the syntax for defining a deconstructor method −
public void Deconstruct(out Type1 param1, out Type2 param2, out Type3 param3) {
param1 = this.Property1;
param2 = this.Property2;
param3 = this.Property3;
}
Following is the syntax for using deconstructors −
(var variable1, var variable2, var variable3) = objectInstance; // or (Type1 variable1, Type2 variable2, Type3 variable3) = objectInstance;
Basic Deconstructor Example
Example
using System;
public class Employee {
public Employee(string employeename, string firstName, string lastName) {
Employeename = employeename;
FirstName = firstName;
LastName = lastName;
}
public string Employeename { get; }
public string FirstName { get; }
public string LastName { get; }
public void Deconstruct(out string employeename, out string firstName, out string lastName) {
employeename = Employeename;
firstName = FirstName;
lastName = LastName;
}
}
class Program {
public static void Main() {
Employee employee = new Employee("emp", "fname", "lname");
(string EName, string Fname, string Lname) = employee;
Console.WriteLine(EName);
Console.WriteLine(Fname);
Console.WriteLine(Lname);
}
}
The output of the above code is −
emp fname lname
Multiple Deconstructors
You can define multiple Deconstruct methods with different numbers of out parameters. C# will automatically choose the appropriate one based on the number of variables you're deconstructing into −
Example
using System;
public class Point {
public double X { get; }
public double Y { get; }
public double Z { get; }
public Point(double x, double y, double z) {
X = x;
Y = y;
Z = z;
}
// Two-parameter deconstructor
public void Deconstruct(out double x, out double y) {
x = X;
y = Y;
}
// Three-parameter deconstructor
public void Deconstruct(out double x, out double y, out double z) {
x = X;
y = Y;
z = Z;
}
}
class Program {
public static void Main() {
Point point = new Point(10.5, 20.3, 15.7);
// Uses two-parameter deconstructor
(var x, var y) = point;
Console.WriteLine($"2D: X={x}, Y={y}");
// Uses three-parameter deconstructor
(var px, var py, var pz) = point;
Console.WriteLine($"3D: X={px}, Y={py}, Z={pz}");
}
}
The output of the above code is −
2D: X=10.5, Y=20.3 3D: X=10.5, Y=20.3, Z=15.7
Using var and Discard Pattern
You can use var for type inference and the discard pattern (_) to ignore values you don't need −
Example
using System;
public class Person {
public string Name { get; }
public int Age { get; }
public string City { get; }
public Person(string name, int age, string city) {
Name = name;
Age = age;
City = city;
}
public void Deconstruct(out string name, out int age, out string city) {
name = Name;
age = Age;
city = City;
}
}
class Program {
public static void Main() {
Person person = new Person("Alice", 30, "New York");
// Using var for type inference
(var name, var age, var city) = person;
Console.WriteLine($"Person: {name}, {age}, {city}");
// Using discard pattern to ignore city
(var personName, var personAge, _) = person;
Console.WriteLine($"Only name and age: {personName}, {personAge}");
}
}
The output of the above code is −
Person: Alice, 30, New York Only name and age: Alice, 30
Conclusion
Deconstructors in C# 7.0 provide an elegant way to extract multiple values from objects using tuple syntax. They enable cleaner code when working with objects that naturally represent multiple related values, and support multiple overloads and flexible assignment patterns.
