currently in c#7 (version 15.3.4) following code is valid to compile but both variables are legitimately unusable.
switch(fruit)
{
case Apple apple:
case Orange orange:
// impossible to use apple or orange
break;
case Banana banana:
break;
}
If you try to use them, you get familiar error, variable might not be initialized before accessing.
Some times in pattern matching you don't care about exact type, as long as that type is in category that you want. here only apples and oranges as an example.
List<Fruit> applesAndOranges = new List<Fruit>();
switch(fruit)
{
case Fruit X when X is Apple || X is Orange:
applesAndOranges.Add(X);
break;
case Banana banana:
break;
}
Are there better approaches?