Lets say you had something that was going to stay changed for a while, like the game state of of a game. In this game, the game loop runs constantly, and it’s update method runs constantly.
You can accomplish a game state by using a switch statement with a variable that represents the game state, and depending on it’s value, it will execute code for the menu or gameplay or another state of the game.
Every game update, it will go through this switch statement first, and this seems inefficient
Is there a more efficient alternative to the switch statement for stuff that is not going to change for a while? I can see this being done with a variable for a method, but c# does not support method variables I think.
Standard approach to change switch into something is to use Dictionary of choice-to-handler. This is discussed here Refactor my C# code – Switch statement
Another approach is to use ploymorphism and replace each choice with class handling it. Check out http://hanuska.blogspot.com/2006/08/swich-statement-code-smell-and.html