Basic C#

Posted on 2011-10-10
Last Modified: 2013-12-16
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
Question by:kensy11
    LVL 16

    Accepted Solution

    writeline OUTPUTs. Use ReadKey() for input. (single key)

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

    LVL 2

    Assisted Solution


    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now