Different methods to read a character in C#

Peruse a character in C#: Here, we will figure out how to peruse a character by utilizing various techniques in C#?

As we realize that, Console.ReadLine() is utilized for contribution to C#, it really peruses a string and afterwards, we change over or parse it too focused kind.

In this instructional exercise, we will figure out how to peruse a character in C#?

Techniques to peruse/input single character in C#

Following techniques can be utilized to peruse a character:

  1. Utilizing Console.ReadLine()[0]
  2. Utilizing Console.ReadKey().KeyChar
  3. Utilizing Char.TryParse()
  4. Utilizing Convert.ToChar()

1) Character input utilizing Console.ReadLine()[0]

It’s basic, as we realize that Console.ReadLine() peruses a string and string is the arrangement of characters. So we can utilize this technique and concentrate its first character utilizing 0th Index ([0]). For this situation, we can include a solitary character and string likewise – it will return just first character.

Syntax:

    char_variable = Console.ReadLine()[0];

Example: C# code to Read a character utilizing Console.ReadLine()[0]

// C# program to input character
// using Console.ReadLine()[0]
using System;
using System.IO;
using System.Text;

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            char ch;
            
            //input character 
            Console.Write("Enter a character: ");
            ch = Console.ReadLine()[0];
         
            //printing the input character
            Console.WriteLine("Input character is {0}", ch);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

First run:
Enter a character: H
Input character is H

Second run:
Enter a character: Hello world
Input character is H

2) Character input utilizing Console.ReadKey().KeyChar

We can likewise utilize Console.ReadKey() strategy to peruse a key and afterward to get the character, use KeyChar.

Console.ReadKey() – is utilized to get the following character or capacity key squeezed by the client. The squeezed key will show on the support.

KeyChar restores the Unicode character spoke to by the current System.ConsoleKeyInfo object.

Note: as such, if it’s not too much trouble comprehend – it peruses a capacity key (a character additionally), shows on the reassure, yet don’t hold on to squeeze return key (for example ENTER).

Syntax:

    char_variable = Console.ReadKey().KeyChar;

Example: C# code to Read a character utilizing Console.ReadKey().KeyChar

// C# program to input a character 
// using Console.ReadKey().KeyChar
using System;
using System.IO;
using System.Text;

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            char ch;
            
            //input character 
            Console.Write("Enter a character: ");
            ch = Console.ReadKey().KeyChar;
         
            //printing the input character
            Console.WriteLine("Input character is {0}", ch);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

Enter a character: HInput character is H

3) Character input utilizing Char.TryParse(string, out)

Char.TryParse() technique is the ideal strategy to peruse a character as it additionally handles the exemption for example on the off chance that info esteem isn’t a character it won’t toss any blunder. It restores an info status likewise if character input is legitimate – it returns genuine, else it returns bogus.

Syntax:

    bool result = Char.TryParse(String s, out char char_variable);

It stores the outcome in char_variable and restores a Boolean worth.

Example: C# code to Read a character utilizing Char.TryParse():

// C# program to input a character
// using Char.TryParse() 
using System;
using System.IO;
using System.Text;

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            char ch;
            bool result;
            
            //input character 
            Console.Write("Enter a character: ");
            result = Char.TryParse(Console.ReadLine(), out ch);
         
            //printing the input character
            Console.WriteLine("result is: {0}", result);
            Console.WriteLine("Input character is {0}", ch);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

First run:
Enter a character: A
result is: True
Input character is A

Second run:
Enter a character: Hello world
result is: False
Input character is

4) Character input utilizing Convert.ToChar()

Convert.ToChar() technique changes over the predetermined string’s an incentive to the character.

Note: Input esteem must be a solitary character on the off chance that you input a string – it will toss an exemption “String must be actually one character long”.

Syntax:

    char_variable = Convert.ToChar(string s);

Example: C# code to Read a character utilizing Convert.ToChar()

// C# program to input a character
// using Convert.ToChar()
using System;
using System.IO;
using System.Text;

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            char ch;
            
            //input character 
            Console.Write("Enter a character: ");
            ch = Convert.ToChar(Console.ReadLine());
         
            //printing the input character
            Console.WriteLine("Input character is {0}", ch);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

First run:
Enter a character: H
Input character is H

Second run: 
Enter a character: Hello world
Exception throws...

Leave a Comment

error: Alert: Content is protected!!