表达身体构造函数 - 多个变量

时间:2017-07-20 16:09:02

标签: variables constructor expression

Microsoft的Expression Bodied Constructors(https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members)示例显示以下内容,显然有效:

public class Location
{
     private string locationName;

     public Location(string name) => locationName = name;
}

但是当我尝试以下内容时,不要去:

public class Location
{
  private string locationName;
  private string locationCountry;

//but what about two variables?
// (the below code does not work, I get "Error CS8179 Predefinedtype 'System.ValueTuple`2' is not defined or imported)

public Location(string ln, string lc) => (locationName, locationCountry) = (ln, ld);
}

0 个答案:

没有答案