Coding Tips & Tricks


Home>C#

Hidden Gems inside .Net Classes

Surprisingly, there are loads of great data snippets hidden inside .Net classes. If you've ever found yourself adding localization to your website, you may have come across the CultureInfo Class in .Net. I know that sometimes I have needed to get a list of country ISO codes and have struggled to get a comprehensive list, or I have needed to retrieve the list from a Database containing the names. This can add an extra Database call and could result in extra code maintenance, and possibly a performance hit.

Well, this is where CultureInfo comes to the rescue. Using this Class, you will be able to easily retrieve a list of country names and their ISO codes using built-in .Net code.

Which gives you a list of 128 countries and their region information:

Net Culture Names

We can then use the RegionInfo Class to retrieve native names, currencies and ISO codes:

This gives you:

CultureInfo Currency Native Name

You can also retrieve the month names and day names:

CultureInfo Month Names

Okay, so all of these are in English...what about other languages? You simply need to set the culture and you will be able to see the results in your chosen language.

CultureInfo Day Names in French

Another really handy class is the TimeZoneInfo class, it allows you to retrieve a list of time zones along with their names and values natively using .Net. It can be used to represent any time zone in the world.

TimeZoneInfo Time Zones

Whoa! No need to store and maintain a list of cultures, day, month or timezone names any more. Much better!








Comments

Rui Jarimba - 7/31/2012
Good stuff Dean, Thanks for sharing! Regards, Rui Jarimba

Dean - 8/1/2012
@Rui - Thank you Rui!

Bevan Arps - 8/1/2012
But aren't there way more than 128 countries? Like over 270? Seems a flawed technique for that reason alone.

Oded - 8/1/2012
Nice list of some of the goodies in the System.Globalization namespace. These make getting localized currency symbols, month names and more very easy, unless the wanted locale is not in the list... Nice one!

JJ Rock - 8/1/2012
@Bevan Arps: I believe this is due to the code specifying to retrieve only CultureTypes.SpecificCultures. See http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.getcultures.aspx

James Curran - 8/1/2012
(from cl in CultureInfo.GetCultures(CultureTypes.SpecificCultures) let rg = new RegionInfo(cl.Name) orderby rg.EnglishName select new {rg.EnglishName, rg.Name}) .ToLookup(rg=>rg.EnglishName, rg=>rg.Name)

Daniel Gonzalez - 8/28/2012
But I seem to recall that there are some problems with time zone definitions (or was i the internal naming? I can't remember clearly). But definitely, the completeness of the ISO codes for both countries and currencies is far from complete...

Daniel Gonzalez - 8/28/2012
Support for currencies in the framework can be found in the OSS library http://code.google.com/p/nmoneys/

sandeep ramani - 9/6/2012
Excellent post

Filip - 9/6/2012
Great findings :)

Raj - 9/10/2012
Cool stuff. Thanks

Chris - 2/1/2013
This is very neat, and certainly sufficient for most applications. However, keep in mind that anything from time zones to currencies and countries can change over time, so regular updates and change management can be tricky if you need to do this for something like financial apps, etc.


Add your comment

300 Characters left


Please fill this in to confirm that you are human