En Clases, el deconstructor se usa en forma de métodos para acceder a las variables de clase fuera de la clase asignándolas a nuevas variables. Se logra mediante el uso de parámetros porque al usar los parámetros puede tener múltiples sobrecargas para diferentes valores. Además, puede usar varios métodos de deconstrucción en el mismo programa con la misma cantidad de parámetros de salida o el mismo número y tipo de parámetros de salida en un orden diferente, pero tenga cuidado con este tipo de métodos de deconstrucción múltiple porque causan mucho. de confusión
Sintaxis:
// Creating a deconstructor method public void Deconstruct( out T var1, out T var2, ..., out T varN) { // Code.. } // Deconstructor assignment (T var1, ..., T varN) = obj // Using discards var(_, var2, _, var4) = obj
Aquí T son los tipos de las variables. Entendamos el concepto de deconstructores con la ayuda de los ejemplos dados:
Ejemplo 1:
// C# program to illustrate the concept // of deconstruction with class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { // Geeks class public class Geeks { // Creating variables string Aname; int age; int Tarticals; string language; double salary; // Method public Geeks( string _AuthorName, int _Age, int _TotalArticals, string _Language, double _Salary) { age = _Age; Aname = _AuthorName; Tarticals = _TotalArticals; language = _Language; salary = _Salary; } // Deconstructor public void Deconstruct( out string _AuthorName, out int _Age, out int _TotalArticals, out string _Language, out double _Salary) { _AuthorName = Aname; _Age = age; _TotalArticals = Tarticals; _Language = language; _Salary = salary; } } public class GFG { // Main method static public void Main() { // Creating object of Geeks class Geeks obj = new Geeks( "Sona", 20, 120, "Scala", 40000.0); // Deconstruct the instance of // the Geeks class named as obj (string AuthorName, int Age, int TotalArticals, string Language, double Salary) = obj; // Displayin the values Console.WriteLine("Details of the Author:"); Console.WriteLine("Author age:{0}", Age); Console.WriteLine("Author Name:{0}", AuthorName); Console.WriteLine("Total number of articles:{0}", TotalArticals); Console.WriteLine("Language:{0}", Language); Console.WriteLine("Salary :{0}", Salary); Console.ReadLine(); } } }
Producción:
Ejemplo 2:
// C# program to illustrate the concept of // multiple deconstruction with class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { // Geeks class public class Geeks { // Creating variables string Afname; string Alname; string city; int age; int Tarticals; string language; double salary; // Method public Geeks( string _AuthorFirstName, string _AuthorLastName, string _City, int _Age, int _TotalArticals, string _Language, double _Salary) { age = _Age; Afname = _AuthorFirstName; Alname = _AuthorLastName; city = _City; Tarticals = _TotalArticals; language = _Language; salary = _Salary; } // Deconstructor 1 public void Deconstruct(out string _AuthorFirstName, out string _AuthorLastName, out string _City) { _AuthorFirstName = Afname; _AuthorLastName = Alname; _City = city; } // Deconstructor 2 public void Deconstruct( out int _Age, out int _TotalArticals, out string _Language, out double _Salary) { _Age = age; _TotalArticals = Tarticals; _Language = language; _Salary = salary; } } public class GFG { // Main method static public void Main() { // Creating object of Geeks class Geeks obj = new Geeks( "Sona", "Singh", "Jaipur", 20, 120, "C#", 40000.0); // Deconstruct the instance of // the Geeks class named as obj ( int Age, int TotalArticals, string Language, double Salary) = obj; (string AuthorFirstName, string AuthorLastName, string City) = obj; // Displaying the values Console.WriteLine("Details of the Author:"); Console.WriteLine("First Name:{0}", AuthorFirstName); Console.WriteLine("Last Name:{0}", AuthorLastName); Console.WriteLine("Age:{0}", Age); Console.WriteLine("City Name:{0}", City); Console.WriteLine("Total number of articles:{0}", TotalArticals); Console.WriteLine("Language:{0}", Language); Console.WriteLine("Salary :{0}", Salary); Console.ReadLine(); } } }
Producción:
Publicación traducida automáticamente
Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA