In this tutorial, we aim to familiarize you with the core concepts of input and output (I/O) in C#. The ability to interact with users by reading input and displaying output is fundamental to almost all programming tasks. By the end of this tutorial, you will learn how to:
Prerequisites: Basic knowledge of C# syntax and programming concepts.
In C#, we use the Console
class within the System
namespace for basic input and output operations.
To read input from the user, we use the Console.ReadLine()
method. It reads the next line of characters from the standard input stream.
Console.WriteLine("Enter your name:");
string name = Console.ReadLine(); // Reads input from the user
To write output to the console, we use the Console.WriteLine()
method. It writes the specified data, followed by the current line terminator, to the standard output stream.
string name = "John Doe";
Console.WriteLine("Hello, " + name); // Writes output to the console
// Ask the user for their name
Console.WriteLine("Enter your name:");
string name = Console.ReadLine(); // Reads input from the user
// Display a greeting message
Console.WriteLine("Hello, " + name); // Writes output to the console
In the above code, we first prompt the user to enter their name with Console.WriteLine()
. We then read the user's input using Console.ReadLine()
, and finally display a greeting message with their name.
Expected output:
Enter your name:
John Doe
Hello, John Doe
// Ask the user for two numbers
Console.WriteLine("Enter the first number:");
int num1 = Convert.ToInt32(Console.ReadLine()); // Convert string input to integer
Console.WriteLine("Enter the second number:");
int num2 = Convert.ToInt32(Console.ReadLine()); // Convert string input to integer
// Calculate the sum of the two numbers
int sum = num1 + num2;
// Display the result
Console.WriteLine("The sum is " + sum);
In this code, we read two numbers from the user, convert them to integers, calculate their sum, and display the result.
Expected output:
Enter the first number:
5
Enter the second number:
10
The sum is 15
In this tutorial, you learned how to read input from the user and write output to the console in C#. You also saw practical examples of these operations.
Next steps would be to explore more complex I/O operations, such as file I/O. Check out the official Microsoft documentation for more information.
Write a program that asks the user for their name and age, then displays this information.
Write a program that reads two integers from the user, multiplies them, and displays the result.
Write a program that reads a line of text from the user, then displays the number of characters in that line.
Console.WriteLine("Enter your name:");
string name = Console.ReadLine();
Console.WriteLine("Enter your age:");
string age = Console.ReadLine();
Console.WriteLine("Your name is " + name + " and you are " + age + " years old.");
Console.WriteLine("Enter the first number:");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number:");
int num2 = Convert.ToInt32(Console.ReadLine());
int product = num1 * num2;
Console.WriteLine("The product is " + product);
Console.WriteLine("Enter a line of text:");
string text = Console.ReadLine();
int count = text.Length;
Console.WriteLine("The line contains " + count + " characters.");
Remember, the key to mastering programming is consistent practice. Keep coding!