什么是C#7.0中的解构函数?
C#允许在同一程序中以相同数量的out参数或以不同顺序使用相同数量和类型的out参数使用多个解构方法。
它是新的tuple语法的一部分-与Tuple<>类无关,但取自函数式编程。
Deconstruct关键字用于解构函数
示例
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; System.Console.WriteLine(EName); System.Console.WriteLine(Fname); System.Console.WriteLine(Lname); Console.ReadLine(); } }
输出结果
emp fname lname