C# Convert

C# Convert is an class with many overloading static methods and these methods are designed to Convert one base type to another one. The supported base types are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String. Based on the type to be converted and target type, each conversion method may generate one of 5 results as following table.

ResultsFrom => ToDescriptionExample
No conversion<Type X> => <Type X>Convert a type to itselfConvert.ToChar('C')
InvalidCastExceptionChar <= => Boolean, Single, Double, Decimal, or DateTimeThrow an InvalidCastExceptionConvert.ToDouble('A')
DateTime <= => <Type Except String>Convert.ToDateTime(123)
FormatExceptionWrong string => Boolean, Char, DateTime or other numeric typesThrow a FormatExceptionConvert.ToBoolean("T R U E")
OverflowExceptionNarrowing numeric conversionThrow an OverflowException if the result is larger than the maximum of the target data type.Convert.ToByte(500)
Convert SuccessfullyExcluding above situationsThe target typed data is returnedConvert.ToBoolean("TRUE")

Example 01-91-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;

public class Program
{
    static void Main()
    {
        bool b = Convert.ToBoolean(false);
        Console.WriteLine("b={0}", b);

        try
        {
            DateTime dt = Convert.ToDateTime('Z');
        }
        catch (InvalidCastException)
        {
            Console.WriteLine("An InvalidCastException is caught");
        }

        try
        {
            char c = Convert.ToChar("ABC");
        }
        catch (FormatException)
        {
            Console.WriteLine("A FormatException is caught");
        }

        try
        {
            byte bt = Convert.ToByte(333);
        }
        catch (OverflowException)
        {
            Console.WriteLine("An OverflowException is caught");
            Console.WriteLine("Byte Maximum={0}", Byte.MaxValue);
        }

        b = Convert.ToBoolean(-1);
        Console.WriteLine("b={0}", b);

        Console.Read();
    }
}

Output

b=False
An InvalidCastException is caught
A FormatException is caught
An OverflowException is caught
Byte Maximum=255
b=True

Explanation

  • Line 7: No conversion happened here because the type of the argument is the same as the returned type.
  • Line 12: An InvalidCastException is thrown because a char cannot be converted to a DateTime type.
  • Line 21: A FormatException is thrown because a string with multiple characters cannot be converted to a single char.
  • Line 30: An OverflowException is thrown because 333 is greater than the maximum number of Byte.
  • Line 38: All the numeric number except 0 can be converted to true and 0 can be converted to false.
  • Convert methods support nearly all the built-in type conversion. Parse and TryParse only convert a string to the target type.
  • Convert methods underneath call parse method if the type to be converted is a string.
  • Convert methods can convert a null to 0 but Parse throws an argumentNullException if the argument is null.
  • Both Convert methods and Parse throw a exception if the conversion fails but TryParse returns false in this situation.
  • TryParse is the best solution to convert a string to other types because it returns the conversion result instead of using try-catch block to catch an exception.

Example 01-91-02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;

public class Program
{
    static void ConvertAString(string s)
    {
        Console.WriteLine("s={0}", s==null ? "null" : s);
        try
        {
            int i = Convert.ToInt32(s);
            Console.WriteLine("Convert Successful: i={0}", i);
        }
        catch (FormatException)
        {
            Console.WriteLine("Convert: FormatException");
        }

        try
        {
            int j = Int32.Parse(s);
            Console.WriteLine("Parse Successful: i={0}", j);
        }
        catch (FormatException)
        {
            Console.WriteLine("Parse: FormatException");
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("Parse: ArgumentNullException");
        }

        int k;
        if (Int32.TryParse(s, out k))
        {
            Console.WriteLine("TryParse: Successful");
        }
        else
        {
            Console.WriteLine("TryParse: Failed");
        }
    }

    static void Main()
    {
        ConvertAString("abc");
        ConvertAString(null);

        Console.Read();
    }
}

Output

s=abc
Convert: FormatException
Parse: FormatException
TryParse: Failed
s=null
Convert Successful: i=0
Parse: ArgumentNullException
TryParse: Failed

Explanation

  • Line 5: The method is trying to convert a string to an Int32.
  • Line 8-30: Both Convert methods and Parse method need a try-catch block in case an exception is thrown.
  • Line 33-40: For TryParse, an if statement is enough.
  • Line 45: Both Convert and Parse throw a FormatException but TryParse just returns false.
  • Line 46: null is converted to 0 by using the Convert method.

We find there is a little different between Convert and Cast when converting a floating numeric type to an integer. Check the example below.

Example 01-91-03

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

public class Program
{
    static void Main()
    {
        int i = (int)8.5D;
        int j = Convert.ToInt32(8.5D);
        double k = Math.Round(8.5D);
        Console.WriteLine("i={0}, j={1}, k={2}", i, j, k);

        i = (int)9.5D;
        j = Convert.ToInt32(9.5D);
        k = Math.Round(9.5D);
        Console.WriteLine("i={0}, j={1}, k={2}", i, j, k);

        Console.Read();
    }
}

Output

i=8, j=8, k=8
i=9, j=10, k=10

Explanation

  • Line 7-15: Both Convert.ToInt32 and Math.Round have the same rounded rules and return the same results in either of the 2 situations.
  • Line 7, 12: When casting a double number to an integer, the decimal part is simply cut off.
  • Line 8, 13: When using Convert.ToInt32, the nearest even integer will be returned if the number to be converted is halfway. So 8.5 is converted to 8 but 9.5 is converted 10.