在C#7.0中,我可以为我的类声明以下解构器:
public class Customer { public string FirstName { get; } public string LastName { get; } public string Email { get; } public Customer(string firstName,string lastName) { FirstName = firstName; LastName = lastName; } public void Deconstructor(out string firstName,out string lastName,out string company) { firstName = FirstName; lastName = LastName; company = "Nop-Templates"; } public void Deconstructor(out string firstName,out string lastName) { firstName = FirstName; lastName = LastName; } }
我想在desconstructor中使用我们的变量而不是直接返回一个元组的想法是这样你可以有不同的解构函数重载.但是,我似乎无法将对象解构为三个变量.我只能将它解构为两个变量.
例如,这不编译:
(string firstName,string lastName,string company) = customer;
我收到这个错误:
“无法将’2’元素的元组解构为’3’变量.”
但这确实有效:
(string firstName,string lastName) = customer;
我错过了什么?