如何忽略开关表达式中的一种元组类型?

c#

我有以下开关表达式:

  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
    };

因此,我知道如果品牌是“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
};


以上是如何忽略开关表达式中的一种元组类型?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>