Basic C#

i am a beginner in c#.

the program should show de pressd key "the pressd key is b" forexample

int I;
Console.WriteLine("press a key");

i know the WriteLine will display the string "press a key" but i dont knwo how i can get the pressd key in to my variable I , so i can show it to the screen "the press key is b" forexample

thank you
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

ToddBeaulieuConnect With a Mentor Commented:
writeline OUTPUTs. Use ReadKey() for input. (single key)

            Console.WriteLine("Press any key:");
            ConsoleKeyInfo key = Console.ReadKey();
            if (key.Key == ConsoleKey.B)
            { }

AJRDevConnect With a Mentor Commented:

You can use the ReadKey() method of the Console class. It returns a ConsoleKeyInfo object and you can access the Key property to see what was pressed. Note that if you want to check if any modifier keys were used (such as Shift, Alt, Ctrl, etc) you will need to perform that test separately.

ConsoleKeyInfo consoleKeyInfo = System.Console.ReadKey(true);

Have a look at the MSDN example at

All Courses

From novice to tech pro — start learning today.