switch case with var for initialize variable in fall through case #2703
Replies: 5 comments
-
Couldn't all such cases simply be handled by extracting a method or local function? void HandleMethod(string resultString, bool condition)
{
if(condition)
DoSomeThing();
DoSomeOtherThingWith(resultString);
}
switch(SomeObj.SomeProperty.SomeEnumValue)
{
case SomeEnum.A:
DoSomeThing();
break;
case SomeEnum.B: HandleMethod(resultString:"B", condition:true); break;
case SomeEnum.C: HandleMethod(resultString:"C", condition:false); break;
case SomeEnum.D when score > 50: HandleMethod (resultString:"D", condition:true); break;
case SomeEnum.D: HandleMethod(resultString:"D", condition:false); break;
case SomeEnum.E: HandleMethod(...); break;
case SomeEnum.F: // a not-fall-through can be back to a normal case
default:
throw new NotImplementedException();
} |
Beta Was this translation helpful? Give feedback.
-
@Richiban Sure, Like a |
Beta Was this translation helpful? Give feedback.
-
In the case of Also, the introduction of a variable in your switch statement conflicts with syntax constraints from other parts of C#, where a variable cannot be multiply declared in different code branches to then be available outside that branch. For example, the following is not legal: if(someCondition)
{
var x = 4;
}
else
{
var x = 5;
}
Console.WriteLine($"x = {x}"); |
Beta Was this translation helpful? Give feedback.
-
@Richiban To check for condition and return new variable is already did something. And it could be a function instead of specific syntax if(SomeObject.SomeProperty.SomeField is var x)
DoSomething(x);
// is functionally equivalence to
bool TryGet(out SomeType result)
{
result = SomeObject.SomeProperty.SomeField;
return result != null;
}
if(TryGet(out var x))
DoSomething(x); So why we need it if anything could be made as local function? I think it should be obvious And for second argument It could be like this switch(SomeObj.SomeProperty.SomeEnumValue)
{
case SomeEnum.A:
DoSomeThing();
break;
case SomeEnum.B var (resultString:"B",condition:true):
case SomeEnum.C var (resultString:"C",condition:false):
case SomeEnum.D when score > 50 var (resultString:"D",condition:true):
case SomeEnum.D var (resultString:"D",condition:false):
if(condition)
DoSomeThing();
DoSomeOtherThingWith(resultString);
break;
default:
throw new NotImplementedException();
}
// is functionally equivalence to
var _cacheSwitch = SomeObj.SomeProperty.SomeEnumValue;
switch(_cacheSwitch)
{
case SomeEnum.A:
DoSomeThing();
break;
case SomeEnum.B:
case SomeEnum.C:
case SomeEnum.D when score > 50:
case SomeEnum.D:
var (resultString,condition) = default((string,bool));
switch(_cacheSwitch)
{
case SomeEnum.B:
(resultString,condition) = ("B",true);
break;
case SomeEnum.C:
(resultString,condition) = ("C",false);
break;
case SomeEnum.D when score > 50:
(resultString,condition) = ("D",true);
break;
case SomeEnum.D:
(resultString,condition) = ("D",false);
break;
}
if(condition)
DoSomeThing();
DoSomeOtherThingWith(resultString);
break;
default:
throw new NotImplementedException();
} |
Beta Was this translation helpful? Give feedback.
-
This can be done using C# 7 pattern matching. switch (SomeObj.SomeProperty.SomeEnumValue) {
case SomeEnum.A:
DoSomeThing();
break;
case SomeEnum.B when "B" is string resultString && true is bool condition:
case SomeEnum.C when (resultString = "C") is string && (condition = false) is bool:
case SomeEnum.D when score > 50 && (resultString = "D") is string && (condition = true) is l:
case SomeEnum.D when (resultString = "D") is string && (condition = false) is bool:
case SomeEnum.E: // ERROR : Use of unassigned local variable ; must adding same variable as above
if (condition)
DoSomeThing();
DoSomeOtherThingWith(resultString);
break;
case SomeEnum.F: // a not-fall-through can be back to a normal case
default:
throw new NotImplementedException();
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Sometimes the logic for
switch
condition could easily fall through with a very little changed in some variable. So I wish that we could appendvar
at the case. And if we made a fall through case it then force all those set of cases must implement the samevar
Beta Was this translation helpful? Give feedback.
All reactions