Basic Input and Output in C#

Learn the fundamentals of input and output in C#, including how to read and write text, integers, and booleans. Understand the difference between Console.ReadLine() and Console.WriteLine(), and how to use them effectively in your programs.

Updated October 18, 2023

When developing applications in C#, it is essential to handle basic input and output operations. In this article, we will cover the basics of reading input from the user and displaying output to the console.

Reading Input from the User

To read input from the user, you can use the Console.ReadLine() method. This method waits for the user to press a key and returns their input as a string. Here’s an example:

string userInput = Console.ReadLine();

You can also use the Console.Read() method to read a single character from the user, like this:

char userInput = Console.Read();

Displaying Output to the Console

To display output to the console, you can use the Console.WriteLine() method. This method writes the specified string to the console and moves the cursor down one line. Here’s an example:

Console.WriteLine("Hello, world!");

You can also use the Console.Write() method to write a single character or string to the console, like this:


Handling Input and Output in C#

To handle input and output in C#, you can use a combination of the methods we’ve discussed. Here’s an example of a simple program that reads input from the user, performs some calculations, and displays the results to the console:

string userInput = Console.ReadLine();
int num1 = int.Parse(userInput);
int num2 = int.Parse(Console.ReadLine());
int result = num1 + num2;
Console.WriteLine($"{num1} + {num2} = {result}");

In this example, we first read input from the user using Console.ReadLine(). We then use the int.Parse() method to convert the string input into an integer. We perform some calculations using the integers, and finally, we display the result to the console using Console.WriteLine().


In this article, we have covered the basics of reading input from the user and displaying output to the console in C#. By using the Console.ReadLine() and Console.WriteLine() methods, you can easily handle basic input and output operations in your C# applications. Remember to use these methods in combination with other C# features, such as int.Parse(), to perform more complex tasks. Happy coding!