FOLLOW US ON:

Code Library: c# Switch Statement for State Names & Abbreviations

As a programmer, sometimes the thing you need most is something painfully simple. It may be a thing you’ve done a hundred times, but if you don’t have a code locker where you keep these snippets of code, it can be maddening trying to track down what program you wrote that bit of code in. That is why we are starting the code library section of Vandelay Web to house some of these important pieces of reusable code to keep us organized and to help out any programmers who just need a quick hit.

Today, we start with something just about every programmer has had to do at one point in time: convert a U.S. state abbreviation to the full state name. Its nothing more than a glorified state lookup table, but one that gets used over and over and over again. Below is the switch statement tripping all those cases written in c#. We’ve housed it inside a method for cleanliness. The switch statement sends back the original abbreviation if for some reason it didn’t resolve (U.S. territory perhaps?). Quick, dirty and on your way.