Different Methods to Read a Character in C#

26 May, 2020
In C#,  we know that Console.Read() method is used to read a single character from the standard output device. And also there are different methods available to read the single character. Following methods can be used for this purpose:
Console.ReadLine()[0] Method
Console.ReadKey().KeyChar Method
Char.TryParse() Method
Convert.ToChar() Method
Since, the Console.ReadLine() method is used to reads a string and string is the set of characters. So the first character can be extract using 0th Index. Thus Console.ReadLine()[0] can be used to read a single/first character.
Syntax:
char_variable = Console.ReadLine()[0];

Example: Read a character using Console.ReadLine()[0]
C#
// C# program to Read a character// using Console.ReadLine()[0]    using System;  public class GFG{          // Main Method    static void Main(string[] args)    {                  char chr;                  // use of Console.ReadLine()[0] method        chr = Console.ReadLine()[0];                  // printing the input character        Console.Write(chr);    }}
 
Console Input:
Geeks

Output:
G

Since, the Console.ReadKey() method is used to obtain the next character or function key pressed by the user. And the KeyChar is used to get the Unicode character represented by the current System.ConsoleKeyInfo object. Thus Console.ReadKey().KeyChar can be used to read a single/first character. Basically, it will read a character or a function and show it on the console but without waiting for the Enter key to press. As soon as you will enter a character output will display on the console.
Syntax:
char_variable = Console.ReadKey().KeyChar;

Example: Read a character using Console.ReadKey().KeyChar
C#
// C# program to Input a character// using Console.ReadKey().KeyChar    using System;using System.IO;using System.Text;  public class GFG{          // Main Method    static void Main(string[] args)    {                  char chr;                  // use of Console.ReadKey().KeyChar method        chr = Console.ReadKey().KeyChar;                  // printing the input character        Console.WriteLine(chr);    }}
 
Console Input:
G
Output:
GG
Char.TryParse() method is used to read a character and it also handles the exception. It will throw an error if any input value not a character. It also returns the input status as true for valid character and false for invalid characters.
Syntax:
bool result = Char.TryParse(String s, out char char_variable);

Example: Read a character using Char.TryParse()
C#
// C# program to Read a character// using Char.TryParse()    using System;using System.IO;using System.Text;  public class GFG{          // Main Method    static void Main(string[] args)    {                  char chr;        bool val;                  // use of Char.TryParse() method        val = Char.TryParse(Console.ReadLine(), out chr);                  //printing the input character        Console.WriteLine("Result: " + val);        Console.WriteLine("Input character: " + chr);    }}
 
Console Input:
G

Output:
Result: True
Input character: G

Convert.ToChar() method is used to convert the specified string’s value to the character. The string must be of length 1 otherwise it will throw an error.
Syntax:
char_variable = Convert.ToChar(string s);

Example: Read a character using Convert.ToChar()
C#
// C# program to Read a character// using Convert.ToChar()  using System;using System.IO;using System.Text;  public class GFG{          // Main Method    static void Main(string[] args)    {                  char chr;                  // use of Convert.ToChar() method        chr = Convert.ToChar(Console.ReadLine());                  // printing the input character        Console.WriteLine(chr);    }}
 
Console Input:
G

Output:
G

C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.