C# Convert ToChar(Object)
Description
Convert ToChar(Object)
converts the value of the specified
object to a Unicode character.
Syntax
Convert.ToChar(Object)
has the following syntax.
public static char ToChar(
Object value
)
Parameters
Convert.ToChar(Object)
has the following parameters.
value
- An object that implements the IConvertible interface.
Returns
Convert.ToChar(Object)
method returns A Unicode character that is equivalent to value, or Char.MinValue if value
is null.
Example
The following example attempts to convert each element in an object array to a Char value.
/*from w ww .j av a 2 s .com*/
using System;
public class MainClass{
public static void Main(String[] argv){
object[] values = { 'r', "s", "asdf", (byte) 83, 77, 123456, 1234567,
new DateTime(2014, 3, 10), (uint) 1934,
(sbyte) -17, 1239.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 code above generates the following result.