• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by FaffyWaffles · Mar 01, 2022 at 04:15 AM · switchenumswitchingswitch-casecase

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".

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FaffyWaffles · Mar 01, 2022 at 04:17 AM 0
Share

Is this one of the few times to use a goto statement?

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Mar 01, 2022 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.

Comment
gjf
Monsoonexe
FaffyWaffles

People who like this

3 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by AticAtac · Mar 01, 2022 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() { ... }


Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FaffyWaffles · Mar 01, 2022 at 01:21 PM 0
Share

I realize this. I was substituting DoCaseX() for Debug.Log()...

avatar image

Answer by xxmariofer · Mar 01, 2022 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;
          }
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check if an enum’s case has changed 4 Answers

!= operator syntax for switch statements. (does not equal operator) 2 Answers

Switch-case with enum 1 Answer

Operator '==' cannot be used! 2 Answers

Switch case using enum javascript 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges