Deconstruction



What is Deconstruction in c#?
C# deconstruction means deconstructing an instance of a class. It is a process of splitting a variable value into parts and storing them into new variables. It is helpful when we want to reinitialize the object of a class.

Make sure all the parameters of the deconstructor are out type.

Example: 

using System;  
namespace Deconstruction
{  
  
    public class Employee{  
           private string Name;  
           private int Age;  
        public Student(string name, int age)  
        {  
            this.Name = name;  
            this.Age= age;  
        }  
// creating deconstruct  
        public void Deconstruct(out string name, out int age)  
        {  
            name  = this.Name;  
            age= this.Age;  
        }  
    }  
  
class DeconstructClass
    {  
        static void Main(string[] args)  
        {  
            var employee = new Employee("Jhon", 26);  
            var (name, age) = employee;  
            Console.WriteLine(name +" "+age);  
        }  
    }  
}  

0 Comment's

Comment Form