Export (0) Print
Expand All

Convert.ToChar Method (Object)

Converts the value of the specified object to a Unicode character.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static char ToChar(
	Object value
)

Parameters

value
Type: System.Object

An object that implements the IConvertible interface.

Return Value

Type: System.Char
A Unicode character that is equivalent to value, or Char.MinValue if value is null.

ExceptionCondition
ArgumentNullException

value is a null string.

InvalidCastException

value does not implement the IConvertible interface.

-or-

The conversion of value to a Char is not supported.

OverflowException

value is less than Char.MinValue or greater than Char.MaxValue.

The result of invoking the IConvertible.ToChar method of the underlying type of value.

The following example attempts to convert each element in an object array to a Char value.

object[] values = { 'r', "s", "word", (byte) 83, 77, 109324, 335812911, 
                    new DateTime(2009, 3, 10), (uint) 1934, 
                    (sbyte) -17, 169.34, 175.6m, null };
char result;

foreach (object value in values)
{
   try {
      result = Convert.ToChar(value);
      Console.WriteLine("The {0} value {1} converts to {2}.", 
                        value.GetType().Name, value, result);
   }
   catch (FormatException e) {
      Console.WriteLine(e.Message);
   }
   catch (InvalidCastException) {
      Console.WriteLine("Conversion of the {0} value {1} to a Char is not supported.",
                        value.GetType().Name, value);
   }
   catch (OverflowException) {
      Console.WriteLine("The {0} value {1} is outside the range of the Char data type.",
                        value.GetType().Name, value);
   }
   catch (NullReferenceException) {
      Console.WriteLine("Cannot convert a null reference to a Char.");
   }
}
// The example displays the following output: 
//       The Char value r converts to r. 
//       The String value s converts to s. 
//       String must be exactly one character long. 
//       The Byte value 83 converts to S. 
//       The Int32 value 77 converts to M. 
//       The Int32 value 109324 is outside the range of the Char data type. 
//       The Int32 value 335812911 is outside the range of the Char data type. 
//       Conversion of the DateTime value 3/10/2009 12:00:00 AM to a Char is not supported. 
//       The UInt32 value 1934 converts to ?. 
//       The SByte value -17 is outside the range of the Char data type. 
//       Conversion of the Double value 169.34 to a Char is not supported. 
//       Conversion of the Decimal value 175.6 to a Char is not supported. 
//       Cannot convert a null reference to a Char.      

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft