Skip to main content

...

....

C# Tutorial - Switch Statement Games [Beginner]


Today, we are going to take a look at Switch on the Code's namesake - the switch statement. More specifically, we are going to take a look at some of the lesser known behaviors of the switch statement in C#, hopefully giving you some new syntactic sugar to use in the process.

You might have thought that it doesn't get much simpler in programming than the switch statement, and you are mostly right. But there are a couple useful things to know about the C# switch statement that fall under the category of advanced. For instance - fall through. The C# switch statement does not allow fall through like the C++ one does, although it does allow a specific variation of fall through.

The fall through shown below, while valid in the C++ world, causes a compile error in C#:
public void DoASwitch(int num)
{
  switch (num)
  {
    case 0:
      Console.WriteLine("I'm in case 0!");
    case 1:
      Console.WriteLine("I'm in case 1!");
      break;
  }
}

//Error: Control cannot fall through from one case label ('case 0:') to another
 
Yes, I know, this isn't an advanced feature at all - this is the lack of an advanced feature! If you are from the C++ world, you are probably wondering why the C# designers got rid of this sometimes very useful feature. Well, don't worry, they didn't leave us all hanging out to dry - the reason I showed what was not allowed is so that what is allowed makes more sense.

In C#, we are allowed to do a very specific type of fall through - although in some ways it is not fall through at all, it is more like case grouping:
public void DoASwitch(int num)
{
  switch (num)
  {
    case 0:
    case 1:
    case 2:
      Console.WriteLine("I'm in case 0, 1 or 2!");
      break;
    case 3:
    case 4:
    case 5:
      Console.WriteLine("I'm in case 3, 4 or 5!");
      break;
  }
}
 
When you group cases together like that, it is essentially like a bunch of ORs. In this case, we are saying "for 0 or 1 or 2, do X, and for 3 or 4 or 5, do Y".

Powerful, but still not as powerful as regular fall through. But don't worry - this next part is the cool one. So the designers of C# didn't want implicit fall through - they felt that it lead to less readable code and more programming mistakes. But they didn't have a problem with the concept of fall through itself - so instead they gave C# a way to define explicit fall through. Although really, it is actually more powerful than fall through. Take a look at the next code block:
public enum State { One, Two, Three, Four };

public void DoASwitch(State mystate)
{
  switch (mystate)
  {
    case State.One:
      Console.WriteLine("I'm in state one!");
      break;
    case State.Two:
      Console.WriteLine("I'm in state two!");
      goto case State.Four;
    case State.Three:
      Console.WriteLine("I'm in state three!");
      goto case State.Two;
    case State.Four:
      Console.WriteLine("I'm in state four!");
      goto case State.One;
  }
}
 
That's right, in C# we have the ability to goto a case label! This means that we can walk all over this switch statement, and go to any case we want at any point - we are no longer limited to falling though the statement. For instance, say I passed in State.Three to this function. It would actually end up hitting every case block, and printing out the following:
I'm in state three!
I'm in state two!
I'm in state four!
I'm in state one!
 
Cool, eh? It doesn't quite stop there, we have a little more excitement. You can also use this goto statement to go to the default case in a switch statement. For instance:
public enum State { One, Two, Three, Four };

public static void DoASwitch(State mystate)
{
  switch (mystate)
  {
    case State.One:
      Console.WriteLine("I'm in state one!");
      break;
    case State.Two:
      Console.WriteLine("I'm in state two!");
      goto default;
    case State.Three:
      Console.WriteLine("I'm in state three!");
      goto default;
    case State.Four:
      Console.WriteLine("I'm in state four!");
      goto default;
    default:
      Console.WriteLine("I'm at the default!");
      break;
  }
}
 
Here, in all cases except for State.One, we go to the default block before leaving the statement.

One thing to watch out for when doing these explicit case jumps - it is easy to get into an infinite loop. In fact, the compiler even allows you to do something as silly as this:
public enum State { One, Two, Three, Four };

public static void DoASwitch(State mystate)
{
  switch (mystate)
  {
    case State.One:
      Console.WriteLine("I'm in state one!");
      goto case State.One;
  }
}
 
And I'm sure you can see as soon as you look at that goto statement that once the program enters that block of code it is not going to leave anytime soon.

Well, that's all for today. If you didn't know about C#'s ability to jump between case blocks, I hope that this info leads to some prettier code - and if you did know, I hope the article wasn't too boring.

Comments

Popular posts from this blog

C# Snippet - Shuffling a Dictionary [Beginner]

Randomizing something can be a daunting task, especially with all the algorithms out there. However, sometimes you just need to shuffle things up, in a simple, yet effective manner. Today we are going to take a quick look at an easy and simple way to randomize a dictionary, which is most likely something that you may be using in a complex application. The tricky thing about ordering dictionaries is that...well they are not ordered to begin with. Typically they are a chaotic collection of key/value pairs. There is no first element or last element, just elements. This is why it is a little tricky to randomize them. Before we get started, we need to build a quick dictionary. For this tutorial, we will be doing an extremely simple string/int dictionary, but rest assured the steps we take can be used for any kind of dictionary you can come up with, no matter what object types you use. Dictionary < String , int > origin = new Dictionary < string , int >();

C# WPF Printing Part 2 - Pagination [Intermediate]

About two weeks ago, we had a tutorial here at SOTC on the basics of printing in WPF . It covered the standard stuff, like popping the print dialog, and what you needed to do to print visuals (both created in XAML and on the fly). But really, that's barely scratching the surface - any decent printing system in pretty much any application needs to be able to do a lot more than that. So today, we are going to take one more baby step forward into the world of printing - we are going to take a look at pagination. The main class that we will need to do pagination is the DocumentPaginator . I mentioned this class very briefly in the previous tutorial, but only in the context of the printing methods on PrintDialog , PrintVisual (which we focused on last time) and PrintDocument (which we will be focusing on today). This PrintDocument function takes a DocumentPaginator to print - and this is why we need to create one. Unfortunately, making a DocumentPaginator is not as easy as

C# WPF Tutorial - Implementing IScrollInfo [Advanced]

The ScrollViewer in WPF is pretty handy (and quite flexible) - especially when compared to what you had to work with in WinForms ( ScrollableControl ). 98% of the time, I can make the ScrollViewer do what I need it to for the given situation. Those other 2 percent, though, can get kind of hairy. Fortunately, WPF provides the IScrollInfo interface - which is what we will be talking about today. So what is IScrollInfo ? Well, it is a way to take over the logic behind scrolling, while still maintaining the look and feel of the standard ScrollViewer . Now, first off, why in the world would we want to do that? To answer that question, I'm going to take a an example from a tutorial that is over a year old now - Creating a Custom Panel Control . In that tutorial, we created our own custom WPF panel (that animated!). One of the issues with that panel though (and the WPF WrapPanel in general) is that you have to disable the horizontal scrollbar if you put the panel in a ScrollV