- Home /
What is a more efficient way to write this Switch Statement?
I'm pretty sure I'm missing something, and that I've done this in a way that avoids duplicate statements in the past. But for the life of me, I can't remember.
public enum CaseType { Case1, Case2, Both};
public CaseType caseType;
public void Foo()
{
switch (caseType)
{
case CaseType.Case1:
Debug.Log("Case 1");
break;
case CaseType.Case2:
Debug.Log("Case 2");
break;
case CaseType.Both:
Debug.Log("Case 1");
Debug.Log("Case 2");
break;
}
}
--also, I'm avoiding if/else statements, given larger enums. say, add 10 more cases and substitute "both" for "all".
Answer by Bunny83 · Mar 01 at 03:09 PM
Your example case is not very descriptive as we can not interpolate upwards when you say you have 10 or more switch cases. We don't know what possible combinations you have in mind. If each case only has a single responsibility but you may have a single case that simply includes all other cases, I would recommend to not use a switch case at all. Manually using a dictionary or array of delegates would be much easier and it would be much clearer what the code actually does.
Note that a switch case is a highlevel construct. For simple continuous integral values, the JIT runtime would in most cases construct a jump table which could be viewed a bit like an array of delegates. The runtime is actually quite clever how it actually implements the switch. If there are continuous values but with a huge gap in between, it usually adds a bit of math and some pre conditions to decide which jump table to use. In the extreme case where the actual case values could not be exploited in such a way, the runtime actually uses a Dictionary behind the scenes. So entering a switch statement would mean it looks up the key / switch value in the dictionary and jumps to the designated code segment.
So if you have the case where you just need to run all cases, one after the other, having them in an array or dictionary would be the easiest solution.
C# doesn't allow cases in a switch statement to fall through (at least not when the case actually contains any code) like you usually can in C or C++. However that would not really help here if your individual cases should run on their own if their label is requested. Using an additional if statement at the end of each case with a goto to the next one is possible but is the worst code design possible.
So while this is possible, I would not recommend it:
switch (caseType)
{
case CaseType.Both:
case CaseType.Case1:
Debug.Log("Case 1");
if (caseType == CaseType.Both)
goto Case2;
break;
case CaseType.Case2: Case2:
Debug.Log("Case 2");
break;
}
This is extremely ugly and if you have many cases there are a huge places where you could mess it up, send yourself into an infinite loop or accidentally skip a case.
Answer by AticAtac · Mar 01 at 07:38 AM
Well if the "Debug.Log(...)" is something more complicated then put them into own methods and call them in the switch:
switch (caseType)
{
case CaseType.Case1:
DoCase1();
break;
case CaseType.Case2:
DoCase2();
break;
case CaseType.Both:
DoCase1();
DoCase2();
break;
}
void DoCase1() { ... }
void DoCase2() { ... }
I realize this. I was substituting DoCaseX() for Debug.Log()...
Answer by xxmariofer · Mar 01 at 02:04 PM
I imagine that you oversimplified your code to make it clear, but there is nothing you cann do to make that code more efficient, if you want to avoid duplucates, you could do something like this, but avoid overenginering your code unless there are clear benefits (in this simplified example there are no, maybe in the real use case there are)
switch (caseType)
{
case CaseType.Both:
case CaseType.Case1:
Debug.Log("Case 1");
continue;
case CaseType.Both:
case CaseType.Case2:
Debug.Log("Case 2");
break;
}
Your answer
Follow this Question
Related Questions
Problem with my weapons switch statement C# 3 Answers
Why would my switch's case be correct but the output is not? 2 Answers
How to check if an enum’s case has changed 3 Answers
Switch-case with enum 1 Answer