Skip to content
Related Articles

Related Articles

C# | Char.TryParse () Method
  • Last Updated : 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 () Method
using 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());
    }
}


Output:

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 () Method
using 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());
    }
}


Output:

False

False
My Personal Notes arrow_drop_up
Recommended Articles
Page :