C# | Char.TryParse () Method - GeeksforGeeks

31 Jan, 2019
In C#, Char.TryParse() is Char class method which is used to convert the value from given string to its equivalent Unicode character. Its performance is better than Char.Parse() method.
Syntax :
public static bool TryParse(string str, out char result)

Parameter:
str: It is System.String type parameter which can contain single character or NULL.
result: This is an uninitialized parameter which is used to store the Unicode character equivalent when the conversion succeeded, or an undefined value if the conversion failed. The type of this parameter is System.Char.
Return Type: The method return True, if successfully converted the string, otherwise return False. So type of this method is System.Boolean.
Note: When string is NULL or Length is equal to 1 then conversion failed.Example 1: Below is an program to demonstrates the use of Char.TryParse() Method .
// C# program to illustrate the// Char.TryParse () Methodusing System;   class GFG {      // Main Method    static public void Main()    {          // Declaration of data type        bool result;        Char value;          // Input Capital letter A        result = Char.TryParse("A", out value);          // Display boolean type result        Console.WriteLine(result);        Console.WriteLine(value.ToString());          // Input Capital letter Z        result = Char.TryParse("Z", out value);          // Display boolean type result        Console.WriteLine(result);        Console.WriteLine(value.ToString());          // Input Symbol letter        result = Char.TryParse("$", out value);          // Display boolean type result        Console.WriteLine(result);        Console.WriteLine(value.ToString());          // Input number        result = Char.TryParse("100", out value);          // Display boolean type result        Console.WriteLine(result);        Console.WriteLine(value.ToString());          // Input small letter z        result = Char.TryParse("z", out value);          // Display boolean type result        Console.WriteLine(result);        Console.WriteLine(value.ToString());    }}
True
A
True
Z
True
$
False

True
z

Example 2: Below is an program to demonstrates the use Char.TryParse() method where the input is not a single character and start with a symbol.
// C# program to illustrate the// Char.TryParse () Methodusing System;  class GFG {      // Main Method    static public void Main()    {          // Declaration of data type        bool result;        Char value;          // Input is  String return false        result = Char.TryParse("GeeksforGeeks", out value);          // Display boolean type result        Console.WriteLine(result);        Console.WriteLine(value.ToString());          // Input letter start with symbol  <        result = Char.TryParse("<N", out value);          // Display boolean type result        Console.WriteLine(result);        Console.WriteLine(value.ToString());    }}
False

False

CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
C# | How to insert an element in an Array?
Convert String to Character Array in C#
Lambda Expressions in C#