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#
// 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
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#
// 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
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
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
Similar Reads
Convert a Character to the String in C# Given a character, the task is to character to the string in C#. Examples: Input : X = 'a' Output : string S = "a" Input : X = 'A' Output : string S = "A" Approach: The idea is to use ToString() method, the argument is the character and it returns string converting Unicode character to the string. /
1 min read
File.OpenRead() Method in C# with Examples File.OpenRead(String) is an inbuilt File class method which is used to open an existing file for reading.Syntax:Â Â public static System.IO.FileStream OpenRead (string path); Parameter: This function accepts a parameter which is illustrated below:Â Â path: This is the specified file which is going to
2 min read
Console.Read() Method in C# Console.Read() Method is used to read the next character from the standard input stream. This method basically blocks its return when the user types some input characters. As soon as the user press ENTER key it terminates. Syntax: public static int Read (); Return Value: It returns the next characte
1 min read
C# | Char.IsControl(String, Int32) Method This method is used to indicates whether the character at the specified position in a specified string is categorized as a control character. Syntax: public static bool IsControl (string s, int index); Parameters: s: It is the String. index: It is the character position in s. Return Value: This meth
3 min read
File.ReadAllBytes() Method in C# with Examples File.ReadAllBytes(String) is an inbuilt File class method that is used to open a specified or created binary file and then reads the contents of the file into a byte array and then closes the file.Syntax:Â Â public static byte[] ReadAllBytes (string path); Parameter: This function accepts a parameter
2 min read
Difference between Console.Read and Console.ReadLine in C# In C#, to take input from the standard input device, the following method are used - Console.Read() and Console.ReadLine() method. Console is a predefined class of System namespace. While Read() and ReadLine() both are the Console Class methods. The only difference between the Read() and ReadLine()
2 min read
Console.ReadKey() Method in C# Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen). There are two methods in th
5 min read
C# | Char.IsPunctuation() Method In C#, Char.IsPunctuation() is a System.Char struct method which is used to check whether an Unicode character can be categorized as a punctuation mark or not. This method can be overloaded by passing different type and number of arguments to it. Char.IsPunctuation(Char) Method Char.IsPunctuation(St
3 min read
Console.ReadLine() Method in C# This method is used to read the next line of characters from the standard input stream. It comes under the Console class(System Namespace). If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key. And if standard input is redirected to a file, th
2 min read
C# | Char.IsLetterOrDigit() Method In C#, Char.IsLetterOrDigit() is a System.Char struct method which is used to check whether a Unicode character can be categorized as a letter or decimal digit. Valid letters and decimal digits will be the members of the UnicodeCategory: UppercaseLetter, LowercaseLetter, TitlecaseLetter, ModifierLet
3 min read