Conversion Classes & CultureInfo Class in C#


This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

Conversion Classes

You can convert one data type to another type using the Convert class. The Convert class has a number of static methods, beginning with To and ending with the target data type for example, ToInt32 or ToByte. If successful, the returned instance is an object of the target data type. Not all Strings and Arrays 645 conversions will be possible when using the Convert method, so use a try-catch block if you think you may get uncertain results. 

Listing 20.35: Convert Class 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConversionClass
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 intExample = 19;

            // the methods inside convert
            Console.WriteLine("Convert.ToString, result = {0}", Convert.ToString(intExample));
            Console.WriteLine("Convert.ToBoolean, result = {0}",
            Convert.ToBoolean(intExample)); //displays True
            Console.WriteLine("Convert.ToByte, result = {0}", Convert.ToByte(intExample));
            Console.WriteLine("Convert.ToChar, result = {0}", Convert.ToChar(intExample));
            Console.WriteLine("Convert.ToDouble, result = {0}", Convert.ToDouble(intExample));
            Console.ReadLine();
        }
    }
}

Output of above program

ConversionClassesFig1.gif

If you want to convert strings into dates, you can use the Parse method for the DateTime data type or you can use the ToDateTime method. Both achieve the same objective. Because users sometimes incorrectly enter dates, don't forget to include appropriate exception handling to make sure that any conversion errors are caught. If you want to provide more control over parsing a date, use the ParseExact method. 

CultureInfo Class 

As shown in Listing 20.36, the CultureInfo class contains cultural information like DisplayName, Calendar, and various official abbreviations. 

Listing 20.36: CultureInfo Class 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ConversionClass
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo c = new CultureInfo("tr");
            Console.WriteLine("The CultureInfo is set to: {0}", c.DisplayName);
            Console.WriteLine("The parent culture is: {0}", c.Parent.DisplayName);
            Console.WriteLine("The three leter ISO language name is: {0}",
            c.ThreeLetterISOLanguageName);
            Console.WriteLine("The default calendar for this culture is: {0}\n\n",
            c.Calendar.ToString());
            Console.ReadLine();
        }
    }
}

Output of above program

ConversionClassesFig2.gif

As demonstrated in Listing 20.37, the RegionInfo class contains regional information, including DisplayName, currency information, and official abbreviations. RegionInfo also contains a static property to retrieve the CurrentRegion. 

Listing 20.37: RegionInfo Class 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ConversionClass
{
    class Program
    {
        static void Main(string[] args)
        {
            RegionInfo r = new RegionInfo("tr");
            Console.WriteLine("The name of this region is: {0}", r.Name);
            Console.WriteLine("The currency symbol for the region is: {0}", r.CurrencySymbol);
            Console.WriteLine("Is this region metric : {0} \n\n", r.IsMetric);
            Console.ReadLine();
        }
    }
}

Output of above program

ConversionClassesFig3.gif

Conclusion

Hope this article would have helped you in understanding the Conversion Classes & CultureInfo Class in C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg

The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Up Next
    Ebook Download
    View all
    Learn
    View all