我有以下开关表达式:
public Size GetSize(string brand, string isGift) => (brand, isGift) switch
{
(brand: "1", isGift: "Y") => Size.Small,
(brand: "1", isGift: "N") => Size.Medium,
(brand: "2") => Size.Big, // in this line I get and error
_ => Size.NoSize
};
Run Code Online (Sandbox Code Playgroud)
因此,我知道如果品牌是“2”,它将始终是Size.Big,我想忽略isGift此语句中的字符串,但出现编译错误。有什么办法可以解决吗?
您可以只使用下划线:
public Size GetSize(string brand, string isGift) => (brand, isGift) switch
{
(brand: "1", isGift: "Y") => Size.Small,
(brand: "1", isGift: "N") => Size.Medium,
(brand: "2", _) => Size.Big,
_ => Size.NoSize
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
110 次 |
| 最近记录: |