Deconstruction in C# involves breaking down an instance of a class. This technique is beneficial when we need to reset an object of a class.
Ensure all the arguments of the destructor are of type out.
Let's see an example.
C# Deconstruction Example
Example
using System;
namespace CSharpFeatures
{
public class Student{
private string Name;
private string Email;
public Student(string name, string email)
{
this.Name = name;
this.Email = email;
}
// creating deconstruct
public void Deconstruct(out string name, out string email)
{
name = this.Name;
email = this.Email;
}
}
class DeconstructExample
{
static void Main(string[] args)
{
var student = new Student("irfan", "[email protected]");
var (name, email) = student;
Console.WriteLine(name +" "+email);
}
}
}