rory.ap
rory.ap

Reputation: 35280

Pattern matching in compound if statements and switches with fall-through

If I have an if statement like if (currentShape is ITable table || currentShape is AutoShape autoShape) I cannot use table or autoShape in the body because I get a CS0165 compiler error.

The same is true for a switch statement with fall-through:

void Foo(object o)
{
    switch (o)
    {
        case int i:
        case string s:
        case Guid g:
            string bar = i?.ToString() ?? s?.ToString() ?? g.ToString(); // cannot use i, s, or g.
            break;
    }
}

I understand why, but I'm left wondering, is this a limitation of pattern matching, i.e. you cannot use it in compound if statements, or is there a correct way to construct the statement so I can use either variable (e.g. by initializing them to null so I can then at least do a null check)?

Upvotes: 6

Views: 1329

Answers (2)

Peter Csala
Peter Csala

Reputation: 22809

Since C# 9, you can do this with a...

switch statement:

switch (o)
{
    case object probe when probe is int or string or Guid:
        string bar = probe.ToString();
        break;
}    

switch expression:

var bar = o switch
{
    int or string or Guid => o.ToString() 
};

Upvotes: 5

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241563

If truly there are two separate things to achieve, don't combine the pattern expressions. Two separate if statements will work better.

if (currentShape is ITable table)
{
    // do something with tables
}

if (currentShape is AutoShape autoShape)
{
    // do something with autoshapes
}

However, your other example illustrates that perhaps there is some common functionality between the conditions. ToString() is probably a bad example, as you could just do:

string? bar = o.ToString(); // doesn't matter the type of object

But let's say that perhaps you want a different format applied depending on the type. In that case, a switch expression could be useful. For example:

string? bar =
    o switch
    {
        int i => i.ToString("D3"), // three digits
        Guid g => g.ToString("N"), // no hyphens
        string s => s,    // no need to call ToString on a string
        _ => o.ToString() // all other types
    };

You also asked about whether the variables could be initialized with nulls so you could do a null check. That wouldn't work for non-nullable types like int. For nullable types, it would cause an extraneous compare operations (first to test the type and assign null, second to test for null). Keeping the expressions separate ensures the minimal number of operations.

Upvotes: 5

Related Questions