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.

If value is not null, this method wraps a call to the IConvertible.ToChar implementation 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.      

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft