Console.ReadKey Method In C#

In this article, I will explain you about Console.ReadKey method in C#.
  • 19619

Introduction

Console programs read input and write output. For reading purpose we can also use Console.ReadKey method. Console.ReadKey method is used to obtain the key pressed by the user in the console window. This method accept the character and also return character.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication11

{

    class Program

    {

        static void Main(string[] args)

        {

            // Write to console window.

            ConsoleKeyInfo s;

            Console.WriteLine("-----------press insert----------");

             s = Console.ReadKey();

            if (s.Key == ConsoleKey.Insert)

            {

                Console.WriteLine("You pressed insert");

            }

            //Console.Read();

            Console.ReadKey();

        }

    }

}

 

ConsoleKeyInfo is used to describes the console key that was pressed.

The output of following program

Clipboard211.jpg

 

© 2020 DotNetHeaven. All rights reserved.