How to ignore one tuple type on switch expression?

Viewed 135

I have the following switch expression:

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

So, I know that if the brand is "2" it will be always Size.Big, i would like to ignore the isGift string in this statement but I´m getting a compilation error. Is there any way to fix it?

1 Answers

You can just use an underscore:

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