Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert.ToChar(String, IFormatProvider) Method in C#
The Convert.ToChar(String, IFormatProvider) method in C# converts the first character of a specified string to a Unicode character. This method uses culture-specific formatting information, though the IFormatProvider parameter is ignored for string-to-char conversion since character conversion doesn't depend on culture.
Syntax
Following is the syntax −
public static char ToChar(string val, IFormatProvider provider);
Parameters
-
val − A string of length 1 or null.
-
provider − An object that supplies culture-specific formatting information. This parameter is ignored for string-to-char conversion.
Return Value
Returns a Unicode character that is equivalent to the first and only character in val.
Examples
Basic String to Character Conversion
using System;
using System.Globalization;
public class Demo {
public static void Main() {
CultureInfo cultures = new CultureInfo("en-US");
string val = "Z";
Console.WriteLine("Converted char value...");
char res = Convert.ToChar(val, cultures);
Console.WriteLine("Result: {0}", res);
}
}
The output of the above code is −
Converted char value... Result: Z
Converting Special Characters
using System;
using System.Globalization;
public class Demo {
public static void Main() {
CultureInfo provider = new CultureInfo("fr-FR");
string[] values = {"@", "#", "$", "&", "A", "9"};
Console.WriteLine("Converting strings to characters:");
foreach (string val in values) {
try {
char result = Convert.ToChar(val, provider);
Console.WriteLine("'{0}' -> '{1}'", val, result);
}
catch (Exception ex) {
Console.WriteLine("Error converting '{0}': {1}", val, ex.GetType().Name);
}
}
}
}
The output of the above code is −
Converting strings to characters: '@' -> '@' '#' -> '#' '$' -> '$' '&' -> '&' 'A' -> 'A' '9' -> '9'
Handling Invalid Input
using System;
using System.Globalization;
public class Demo {
public static void Main() {
CultureInfo provider = new CultureInfo("en-US");
string[] testValues = {"X", "Hello", "", null};
foreach (string val in testValues) {
try {
char result = Convert.ToChar(val, provider);
Console.WriteLine("'{0}' converted to: '{1}'", val ?? "null", result);
}
catch (ArgumentNullException) {
Console.WriteLine("null string cannot be converted");
}
catch (FormatException) {
Console.WriteLine("'{0}' is invalid - string length must be 1", val);
}
}
}
}
The output of the above code is −
'X' converted to: 'X' 'Hello' is invalid - string length must be 1 '' is invalid - string length must be 1 null string cannot be converted
Key Points
-
The input string must have exactly one character − empty strings or strings with multiple characters throw
FormatException. -
Passing
nullthrowsArgumentNullException. -
The
IFormatProviderparameter is ignored since character conversion is culture-independent. -
This method is useful when you need to convert single-character strings to their character equivalents.
Conclusion
The Convert.ToChar(String, IFormatProvider) method provides a safe way to convert single-character strings to Unicode characters. While the IFormatProvider parameter is ignored, this overload maintains consistency with other Convert methods that do use culture-specific formatting.
