I have the following code:

using System;

namespace MyApp {
class KeyBoardInputTest {
    static void Main() {
        Console.Write("Enter a character >\t");
        char ch = (char)Console.Read();
        Console.WriteLine("");

        if(Char.IsLetter(ch)) {
            if(Char.IsUpper(ch))
                Console.WriteLine("You have entered an upper case character");
            else
                Console.WriteLine("You have entered a lower case character");
        }
        else if(Char.IsNumber(ch))
            Console.WriteLine("You have entered a numeric character");
        else
            Console.WriteLine("You have entered a non alpha-numeric character");
        Console.Read();
    }
}
} 

After taking an input such as a it is straightaway showing result without waiting for me to press Enter

Using Console.ReadLine() gives the error -

error CS0030: Cannot convert type 'string' to 'char'

Using Console.ReadKey() gives the error -

error CS0030: Cannot convert type 'System.ConsoleKeyInfo' to 'char'
4

There are 4 best solutions below

5
On BEST ANSWER

For example try-parse:

Console.Write("Enter a character >\t");
char ch;
char.TryParse(Console.ReadLine(), out ch);
Console.WriteLine();

You will get the null character '\0' if the user types too many characters, or no characters, before Enter. Alternatively, you can pick up the return value of TryParse, a boolean telling whether the parsing succeeded.

2
On

Consider pivoting your code to accept input as arguments and convert the input to a char using the System class Convert.ToChar()

using System;

namespace MyApp {

class KeyBoardInputTest {
static void Main(string [] args) {    // This line has been changed
    Console.Write("Enter a character >\t");
    char ch = Convert.ToChar(args[0])   // This line has been changed
    Console.WriteLine("");

    if(Char.IsLetter(ch)) {
        if(Char.IsUpper(ch))
            Console.WriteLine("You have entered an upper case character");
        else
            Console.WriteLine("You have entered a lower case character");
    }
    else if(Char.IsNumber(ch))
        Console.WriteLine("You have entered a numeric character");
    else
        Console.WriteLine("You have entered a non alpha-numeric character");
    Console.Read();
}
}
} 
0
On

Just grab the first character of the String which Console.ReadLine() returns

namespace MyApp {
class KeyBoardInputTest {
    static void Main() {
        Console.Write("Enter a character >\t");
        char ch = Console.ReadLine()[0];
        Console.WriteLine("");

        if(Char.IsLetter(ch)) {
            if(Char.IsUpper(ch))
                Console.WriteLine("You have entered an upper case character");
            else
                Console.WriteLine("You have entered a lower case character");
        }
        else if(Char.IsNumber(ch))
            Console.WriteLine("You have entered a numeric character");
        else
            Console.WriteLine("You have entered a non alpha-numeric character");
        Console.Read();
    }
}
} 

Keep in mind this removes any additional characters the user may have entered. It will also cause an IndexOutOfRangeException if you the user does not enter any information.

0
On

For a Dirtier, but very flexible code, you can use Try-Catch to get the input.

Make sure that you initialize the variable with something like: ch = 'c';

Then use this piece of code:

    try()          //You can get the try-catch code snippet by typing 'try' and hit enter or tab
    {
       ch = Convert.ToChar(Console.ReadLine());
    }
    catch(Exception e)
    {
       Console.WriteLine("Enter a Valid Character");  //You can also recurse your function after this
    }

This method is a bit Unclean, but you still can do a lot more in it as compared to other ways. (Like calling a function inside 'catch' or 'try').