Using a template like C # 7.0 without a variable

Assuming that I have Child1 Baseand Child1, Child2, Child3I have the following code:

Base b; // value is acquired
switch (b) 
{
    case Child1 child1:
        ProcessChild1(child1);
        break;

    case Child2 child2:
    case Child3 child3:
        ProcessAnyOther(b); // <--
        break;

    default:
        throw new ArgumentOutOfRangeException(nameof(b));
}

Please note that in the commented line I do not need these variables child2, child3since it does not matter what type it has if it is not child1.
Resharper offers me that an unused variable can be safely deleted. This is where the interesting part begins.

  1. I can not do it:

    case Child2:
    case Child3:
    

    as this leads to the syntax error "class name is currently invalid".
    This use seems the most suitable for me.

  2. I can not do it:

    case Child2 nevermind:
    case Child3 nevermind:
    

    " ". , , ProcessAnyOther ( Child2 Child3) nevermind b.

  3. , :

    case Child2 _:
    case Child3 _:
    

    "_". , .

: ? ? "_" ? #?

+6
1

discard , # 7.

:

- , , . " " . , _ - , var, _ (.. .)

_, , , , .

+7

All Articles