📜  C#7.0 Deconstruction解析

📅  最后修改于: 2020-11-01 03:15:56             🧑  作者: Mango

C#解析

C#解析是对类实例进行解构的过程。当我们要重新初始化类的对象时,这将很有帮助。

确保解构函数的所有参数均为out类型。

让我们来看一个例子。

C#解析示例

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", "irfan@abc.com");
            var (name, email) = student;
            Console.WriteLine(name +" "+email);
        }
    }
}