C# Char TryParse() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Char

Definition

In C#, Char.TryParse attempts to convert a string to a char. It returns true only when the string has exactly one character.

On failure it returns false and sets the out value to a default char—no exception. Prefer TryParse over Parse for user input.

This is a static member of System.Char. You call it with the type name, such as Char.TryParse(...) or char.TryParse(...). A char holds one 16-bit UTF-16 code unit.

Syntax

Pass the string and an out char variable.

C#
if (Char.TryParse(text, out char c)) {
    Console.WriteLine(c);
}
CallWhat it does
Char.TryParse(s, out c)True when s has length 1; c receives the char.

Empty, null, or multi-character strings return false.

Library declarations
C#
public static bool TryParse(string s, out char result);

Parameters

ParameterDescription
sString to parse.
resultOut parameter receiving the char when successful.

Return Value

Return ValueDescription
trueParsed successfully.
falseNot a length-1 string.

C# Examples

Example 1 — Parse "Z" Safely

Succeed with a one-character string.

C#
using System;

class Program {
    static void Main() {
        if (Char.TryParse("Z", out char c)) {
            Console.WriteLine("ok: " + c);
        } else {
            Console.WriteLine("fail");
        }
    }
}

How It Works

  1. Length one. "Z" is valid.
  2. Out char. c receives Z.
  3. True branch. Prints ok.
  4. No throw. Failure would take the else path instead.

Example 2 — Reject Longer Text

Show false for a two-character string.

C#
using System;

class Program {
    static void Main() {
        bool ok = Char.TryParse("Hi", out char c);
        Console.WriteLine(ok);
        Console.WriteLine("result default?: " + (c == '\0'));
    }
}

How It Works

  1. Length two. "Hi" cannot be one char.
  2. False return. Parse failed safely.
  3. Default out. Often '\0' on failure.
  4. Do not use c. Ignore out value when ok is false.

Example 3 — Accept Single Char Codes

Accept only single-character codes from a list.

C#
using System;

class Program {
    static void Main() {
        string[] samples = { "A", "BC", "" };
        foreach (string s in samples) {
            bool ok = Char.TryParse(s, out char c);
            Console.WriteLine((s == "" ? "(empty)" : s) + " -> " + ok + (ok ? (" (" + c + ")") : ""));
        }
    }
}

How It Works

  1. Three samples. Good, long, and empty.
  2. Only A works. Length must be 1.
  3. Empty fails. Nothing to parse.
  4. Batch check. Pattern for validating many inputs.

Frequently Asked Questions

When the string length is exactly 1.
No. Failures return false.
A default char (often ). Do not use it.
Parse throws on bad input; TryParse does not.
TryParse returns false; use Rune/string APIs.

Back to the method index

See all 42 System.Char method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful