C# User Input

C# Programming Guide

In this programming tutorial, we will cover how to perform basic Input/Output (IO) operations in C#. In particular, developers will learn how a user provides input to the system in a .NET and C# application.

Looking to learn C# in an online course environment? We have a list of the Best Online Courses to Learn C# to help get you started.

How to Accept User Input in C#

The simplest method to get input from a user in C# is to use one of these three methods: ReadLine(), ReadKey(), or Read(). They are all contained in the Console class and can be called directly with their class name, as they all are static methods.

C# ReadLine() Method

Let’s start with the most frequently used method to accept user input: ReadLine(). Below is some example code of a .NET Console app built in C# that shows how to get string input from a user with ReadLine():

using System;
namespace InputProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string text;
            Console.Write("Enter string:");
            text = Console.ReadLine();
            Console.WriteLine("You entered : {text}");
        }
    }
}

When you run the above program in your integrated development environment (IDE) or code editor, you get the following result:

Enter string:  Wish I’m on the Everest
You entered: Wish I’m on the Everest

C# Read() and ReadKey() Methods

Like ReadLine(), the Read() and ReadKey() methods are also used for accepting input data from the user; however, there are some differences in the way these two methods manipulate the received input:

  • Read(): This method reads the next character from the C# input stream – or command line – and returns the ASCII value of that character.
  • ReadKey(): This method obtains only the next key from the input stream. It is also used to hold the screen until the user presses a key. An example is to let the user press Y or N to continue in an interactive application.

The following code example illustrates how to get input from the user using the Read() and ReadKey() methods in C#:

Example of Console.ReadKey():
int text;
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.WriteLine();

The above code snippet results in the following output:

Press any key to continue…

Were there further code below the Console.ReadKey() and Console.WriteLine(), it would be triggered once the user pressed any key on their keyboard. Since there is no further code, the program simply exits once the user presses a key.

Here is an example of how to use the Read() method in C#:

Example of Console.Read():
Console.Write("Enter any character - ");
input = Console.Read();
Console.WriteLine("Ascii Value of given character= {0}", input);

Here, the program prompts a user to ”Enter any character – “. It then accepts whatever text the user enters until the user presses the Enter or Return key. It then takes this value and writes it, and the preceding text (ie; “Ascii Value of given character =”) to the screen.

Read: Working with C# Output

How to Read Numerical Values in C#

C# has easy methods for reading strings or characters – all programmers need to do is to call the corresponding methods, like Read(), ReadKey(), or ReadLine() as shown above. However, dealing with numerical values requires a little more effort on behalf of the developer.

In the next code example, we will use the same method – ReadLine() – to receive input as a string, but we will then do a little extra work to convert the string to an integer or floating-point type.

C# has a Convert class that contains several methods to convert a string type into an integer or floating-point type. The following code examples show how to read numerical values in C# from a user’s input:

Example 1
using System;
namespace TypeCastDemoProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            int val;
            Console.Write("Enter an integer value: ");
            input = Console.ReadLine();
            // Converting to integer type
            val = Convert.ToInt32(input);
            Console.WriteLine("You entered: {0}", val);
        }
    }
}

Running this code produces the following output:

Enter an integer value: 
You entered: 

The ToInt32() method we used in the above code snippet belongs to the Convert class, which converts the string provided by the user into an integer type. This process is called typecasting. Note that the output above is blank – if a user were to input data, the converted version would follow the sentence: ”You entered: “.

Here is our second example, showing how to convert to a decimal or floating-point value:

Example 2
using System;
namespace TypeCastDemoProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            double val;
            Console.Write("Enter a double value: ");
            input = Console.ReadLine();
            // Converting to double type
            val = Convert.ToDouble(input);
            Console.WriteLine("You entered {0}", val);
        }
    }
}

Here, the ToDouble() method also belongs to the Convert class, which converts the given string into a double type. You can also convert it to other types, as per your needs. If you right-click on the Convert class and go to its definitions, you will come across different methods that allow a user to typecast the given data into the desired type.

Reading Input and Typecasting in C#

There are alternate ways to read numeric value input in C#. In addition to the above, you can perform both operations – reading input and typecasting it – into a single line, as depicted in the following code:

Console.Write("Enter integer value: ");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You entered {0}", x);
Console.Write("Enter double value: ");
double y = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("You entered {0}", y);

This method is more efficient and requires fewer lines of code than our previous examples.

Read more C# programming tutorials and software development guides.

Tariq Siddiqui
Tariq Siddiqui
A graduate in MS Computer Applications and a Web Developer from India with diverse skills across multiple Web development technologies. Enjoys writing about any tech topic, including programming, algorithms and cloud computing. Traveling and playing video games are the hobbies that interest me most.

More by Author

Previous article
Next article

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read