Different Methods to Read a Character in C#
Last Updated :
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
Console.ReadLine()[0] 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#
using System;
public class GFG{
static void Main( string [] args)
{
char chr;
chr = Console.ReadLine()[0];
Console.Write(chr);
}
}
|
Console Input:
Geeks
Output:
G
Console.ReadKey().KeyChar Method
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#
using System;
using System.IO;
using System.Text;
public class GFG{
static void Main( string [] args)
{
char chr;
chr = Console.ReadKey().KeyChar;
Console.WriteLine(chr);
}
}
|
Console Input:
G
Output:
GG
Char.TryParse() Method
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#
using System;
using System.IO;
using System.Text;
public class GFG{
static void Main( string [] args)
{
char chr;
bool val;
val = Char.TryParse(Console.ReadLine(), out chr);
Console.WriteLine( "Result: " + val);
Console.WriteLine( "Input character: " + chr);
}
}
|
Console Input:
G
Output:
Result: True
Input character: G
Convert.ToChar() Method
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#
using System;
using System.IO;
using System.Text;
public class GFG{
static void Main( string [] args)
{
char chr;
chr = Convert.ToChar(Console.ReadLine());
Console.WriteLine(chr);
}
}
|
Console Input:
G
Output:
G
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...