Ok, admittedly I got carried away implementing this. The source is from the link led mike provided. Specifically, I used this[^] one.
using System;
using System.Collections.Generic;
namespace States
{
public class StateAbbreviations
{
private static readonly Dictionary<string, string> stateAbbreviations = new Dictionary<string, string>();
private static readonly Dictionary<string, string> stateNames = new Dictionary<string, string>();
static StateAbbreviations()
{
stateAbbreviations.Add("ALABAMA", "AL");
stateAbbreviations.Add("ALASKA", "AK");
stateAbbreviations.Add("AMERICAN SAMOA", "AS");
stateAbbreviations.Add("ARIZONA", "AZ");
stateAbbreviations.Add("ARKANSAS", "AR");
stateAbbreviations.Add("CALIFORNIA", "CA");
stateAbbreviations.Add("COLORADO", "CO");
stateAbbreviations.Add("CONNECTICUT", "CT");
stateAbbreviations.Add("DELAWARE", "DE");
stateAbbreviations.Add("DISTRICT OF COLUMBIA", "DC");
stateAbbreviations.Add("FEDERATED STATES OF MICRONESIA", "FM");
stateAbbreviations.Add("FLORIDA", "FL");
stateAbbreviations.Add("GEORGIA", "GA");
stateAbbreviations.Add("GUAM", "GU");
stateAbbreviations.Add("HAWAII", "HI");
stateAbbreviations.Add("IDAHO", "ID");
stateAbbreviations.Add("ILLINOIS", "IL");
stateAbbreviations.Add("INDIANA", "IN");
stateAbbreviations.Add("IOWA", "IA");
stateAbbreviations.Add("KANSAS", "KS");
stateAbbreviations.Add("KENTUCKY", "KY");
stateAbbreviations.Add("LOUISIANA", "LA");
stateAbbreviations.Add("MAINE", "ME");
stateAbbreviations.Add("MARSHALL ISLANDS", "MH");
stateAbbreviations.Add("MARYLAND", "MD");
stateAbbreviations.Add("MASSACHUSETTS", "MA");
stateAbbreviations.Add("MICHIGAN", "MI");
stateAbbreviations.Add("MINNESOTA", "MN");
stateAbbreviations.Add("MISSISSIPPI", "MS");
stateAbbreviations.Add("MISSOURI", "MO");
stateAbbreviations.Add("MONTANA", "MT");
stateAbbreviations.Add("NEBRASKA", "NE");
stateAbbreviations.Add("NEVADA", "NV");
stateAbbreviations.Add("NEW HAMPSHIRE", "NH");
stateAbbreviations.Add("NEW JERSEY", "NJ");
stateAbbreviations.Add("NEW MEXICO", "NM");
stateAbbreviations.Add("NEW YORK", "NY");
stateAbbreviations.Add("NORTH CAROLINA", "NC");
stateAbbreviations.Add("NORTH DAKOTA", "ND");
stateAbbreviations.Add("NORTHERN MARIANA ISLANDS", "MP");
stateAbbreviations.Add("OHIO", "OH");
stateAbbreviations.Add("OKLAHOMA", "OK");
stateAbbreviations.Add("OREGON", "OR");
stateAbbreviations.Add("PALAU", "PW");
stateAbbreviations.Add("PENNSYLVANIA", "PA");
stateAbbreviations.Add("PUERTO RICO", "PR");
stateAbbreviations.Add("RHODE ISLAND", "RI");
stateAbbreviations.Add("SOUTH CAROLINA", "SC");
stateAbbreviations.Add("SOUTH DAKOTA", "SD");
stateAbbreviations.Add("TENNESSEE", "TN");
stateAbbreviations.Add("TEXAS", "TX");
stateAbbreviations.Add("UTAH", "UT");
stateAbbreviations.Add("VERMONT", "VT");
stateAbbreviations.Add("VIRGIN ISLANDS", "VI");
stateAbbreviations.Add("VIRGINIA", "VA");
stateAbbreviations.Add("WASHINGTON", "WA");
stateAbbreviations.Add("WEST VIRGINIA", "WV");
stateAbbreviations.Add("WISCONSIN", "WI");
stateAbbreviations.Add("WYOMING", "WY");
foreach(KeyValuePair<string, string> pair in stateAbbreviations)
{
stateNames.Add(pair.Value, pair.Key);
}
}
public static string GetStateName(string stateAbbreviation)
{
string name;
if(stateAbbreviation == null)
{
throw new ArgumentNullException("State abbreviation cannot be null");
}
else
{
if(!stateNames.TryGetValue(stateAbbreviation.ToUpper(), out name))
{
throw new ArgumentException("Unknown abbreviation");
}
}
return name;
}
public static string GetStateAbbreviation(string stateName)
{
string abbreviation;
if(stateName == null)
{
throw new ArgumentNullException("State name cannot be null");
}
else
{
if(!stateAbbreviations.TryGetValue(stateName.ToUpper(), out abbreviation))
{
throw new ArgumentException("Unknown state");
}
}
return abbreviation;
}
public static IEnumerable<string> Abbreviations
{
get
{
return stateAbbreviations.Values;
}
}
public static IEnumerable<string> Names
{
get
{
return stateNames.Values;
}
}
}
}
Some driver code:
using System;
using System.Collections.Generic;
using System.Text;
namespace States
{
class Program
{
static void Main(string[] args)
{
foreach(string name in StateAbbreviations.Names)
{
Console.WriteLine(StateAbbreviations.GetStateAbbreviation(name));
}
Console.WriteLine();
foreach(string abbreviaton in StateAbbreviations.Abbreviations)
{
Console.WriteLine(StateAbbreviations.GetStateName(abbreviaton));
}
Console.Read();
}
}
}
Written just for fun. Use at your own risk.
|