Is there built in .NET functionality for making state abbreviations out of state names?
I know the function wouldn’t be difficult to write, but I would assume that MS has thought of a more efficient way than the following x50:
if statename.tolower = "new york" then
statename = "NY"
else if
any other thoughts of making this more efficient are also appreciated.
so if you are going to go this route I would:
use a switch statement instead of if else
switch(state.ToLower()). This will be more efficient than if then statements. The compiler will optimize the switch statement.If you absolutely must use an if then statement. Do
string lowerCaseState = state.ToLower().
if(lowerCaseState == “new york”){….}else if…
This way you are creating a lower case string once (strings are immutable) instead of each part of the if then statement.
In truth, I would probably use a switch statement with a static method.
You could create an object to store the values to load them each time the program runs, but why? You might as well let the compiler optimize access for non-changing static values.