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
Char.TryParse() Method in C#
The Char.TryParse() method in C# is used to convert a string representation to its equivalent Unicode character. This method returns true if the conversion succeeds, or false if it fails, making it a safe alternative to Char.Parse() which throws exceptions on invalid input.
The method only succeeds if the input string contains exactly one character. Multi-character strings, empty strings, or null values will cause the method to return false.
Syntax
Following is the syntax for the Char.TryParse() method −
public static bool TryParse(string str, out char result);
Parameters
-
str − A string containing a single character to convert, or null.
-
result − When this method returns, contains the Unicode character equivalent to the single character contained in
str, if the conversion succeeded, or an undefined value if the conversion failed.
Return Value
Returns true if str was converted successfully; otherwise, false.
Using Char.TryParse() with Invalid Input
Example
using System;
public class Demo {
public static void Main() {
bool res;
char ch;
res = Char.TryParse("10", out ch);
Console.WriteLine("Input: "10"");
Console.WriteLine("Result: " + res);
Console.WriteLine("Character: " + ch);
Console.WriteLine();
res = Char.TryParse("", out ch);
Console.WriteLine("Input: "" (empty)");
Console.WriteLine("Result: " + res);
Console.WriteLine("Character: " + ch);
}
}
The output of the above code is −
Input: "10" Result: False Character: Input: "" (empty) Result: False Character:
Using Char.TryParse() with Valid Input
Example
using System;
public class Demo {
public static void Main() {
bool res;
char ch;
res = Char.TryParse("P", out ch);
Console.WriteLine("Input: "P"");
Console.WriteLine("Result: " + res);
Console.WriteLine("Character: " + ch);
Console.WriteLine();
res = Char.TryParse("5", out ch);
Console.WriteLine("Input: "5"");
Console.WriteLine("Result: " + res);
Console.WriteLine("Character: " + ch);
Console.WriteLine("Unicode value: " + (int)ch);
}
}
The output of the above code is −
Input: "P" Result: True Character: P Input: "5" Result: True Character: 5 Unicode value: 53
Common Use Cases
Example
using System;
public class Demo {
public static void Main() {
string[] inputs = { "A", "z", "5", "@", "AB", "", null };
foreach (string input in inputs) {
char result;
bool success = Char.TryParse(input, out result);
string displayInput = input ?? "null";
if (success) {
Console.WriteLine($"'{displayInput}' ? '{result}' (Unicode: {(int)result})");
} else {
Console.WriteLine($"'{displayInput}' ? Parse failed");
}
}
}
}
The output of the above code is −
'A' ? 'A' (Unicode: 65) 'z' ? 'z' (Unicode: 122) '5' ? '5' (Unicode: 53) '@' ? '@' (Unicode: 64) 'AB' ? Parse failed '' ? Parse failed 'null' ? Parse failed
Conclusion
The Char.TryParse() method provides a safe way to convert single-character strings to char values without throwing exceptions. It returns true for successful conversions of exactly one character, and false for multi-character strings, empty strings, or null values.
