Convert Class in C#

In this blog I am trying to explain the concept of Convert Class in C#.

Convert Class

Convert class returns a type whose value is equivalent to the value of a specified type. The supported base types are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String.

A conversion method exists to convert every base type to every other base type. However, the actual call to a particular conversion method can produce one of four outcomes, depending on the value of the base type at run time and the target base type. These four outcomes are:

·         No conversion. This occurs when an attempt is made to convert from a type to itself (for example, by calling Convert.ToInt32(Int32) with an argument of type Int32). In this case, the method simply returns an instance of the original type.

·         An InvalidCastException. This occurs when a particular conversion is not supported. An InvalidCastException is thrown for the following conversions:

o   Conversions from Char to Boolean, Single, Double, Decimal, or DateTime.

o   Conversions from Boolean, Single, Double, Decimal, or DateTime to Char.

o   Conversions from DateTime to any other type except String.

o   Conversions from any other type, except String, to DateTime.

·         A successful conversion. For conversions between two different base types not listed in the previous outcomes, all widening conversions as well as all narrowing conversions that do not result in a loss of data will succeed and the method will return a value of the targeted base type.

·         An OverflowException. This occurs when a narrowing conversion results in a loss of data. For example, trying to convert a Int32 instance whose value is 10000 to a Byte type throws an OverflowException because 10000 is outside the range of the Byte data type.

Code Example

// Sample for the Convert class summary.

1.  using System;

2.  class ConvertorClass

3.  {

4.  public static void Main()

5.  {

6.  bool xBool = true;

7.  short xShort = 1;

8.  int xInt = 2;

9.  long xLong = 3;

10. float xSingle = 4.0f;

11. double xDouble = 5.0;

12. decimal xDecimal = 6.0m;

13. string xString = "7";

14. char xChar = '8'; // '8' = hexadecimal 38 = decimal 56

15. byte xByte = 9;

16. //  The following types are not CLS-compliant.

17. ushort xUshort = 120;

18. uint xUint = 121;

19. ulong xUlong = 122;

20. sbyte xSbyte = 123;

21. Console.WriteLine("Boolean:  {0}", Convert.ToInt64(xBool));

22. Console.WriteLine("Int16:    {0}", Convert.ToInt64(xShort));

23. Console.WriteLine("Int32:    {0}", Convert.ToInt64(xInt));

24. Console.WriteLine("Int64:    {0}", Convert.ToInt64(xLong));

25. Console.WriteLine("Single:   {0}", Convert.ToInt64(xSingle));

26. Console.WriteLine("Double:   {0}", Convert.ToInt64(xDouble));

27. Console.WriteLine("Decimal:  {0}", Convert.ToInt64(xDecimal));

28. Console.WriteLine("String:   {0}", Convert.ToInt64(xString));

29. Console.WriteLine("Char:     {0}", Convert.ToInt64(xChar));

30. Console.WriteLine("Byte:     {0}", Convert.ToInt64(xByte));

31. Console.WriteLine("DateTime: There is no example of this conversion because");

32. Console.WriteLine("  a DateTime cannot be converted to an Int64.");

33. Console.WriteLine("The following types are not CLS-compliant.\n");

34. Console.WriteLine("UInt16:   {0}", Convert.ToInt64(xUshort));

35. Console.WriteLine("UInt32:   {0}", Convert.ToInt64(xUint));

36. Console.WriteLine("UInt64:   {0}", Convert.ToInt64(xUlong));

37. Console.WriteLine("SByte:    {0}", Convert.ToInt64(xSbyte));

38. Console.ReadKey();

39. }

40. }

Note: - The DateTime type cannot be converted to an Int64. (DateTime xDateTime = DateTime.Now;)

Code description:

1.       Using System (include the System namespace).

2.        Create a Convertor class.

4.       Start Mani method.

6.       to 20. Create all types of variable with its initial value.

21.   to 37. Show all variable value with covert into ToInt64(means integer vale with 64 bit) .


Leave Comment