Basic Input and Output in C#

Tutorial 5 of 5

Basic Input and Output in C

1. Introduction

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:

  • Read data from the user using the console.
  • Display information to the console.
  • Understand and implement basic I/O operations in C#.

Prerequisites: Basic knowledge of C# syntax and programming concepts.

2. Step-by-Step Guide

In C#, we use the Console class within the System namespace for basic input and output operations.

Reading Input:

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

Writing Output:

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

Best Practices:

  • Always validate user input to avoid runtime errors and security vulnerabilities.
  • Use meaningful variable names to make your code more readable.

3. Code Examples

Example 1: Basic Input/Output

// 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

Example 2: Simple Addition Program

// 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

4. Summary

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.

5. Practice Exercises

  1. Write a program that asks the user for their name and age, then displays this information.

  2. Write a program that reads two integers from the user, multiplies them, and displays the result.

  3. Write a program that reads a line of text from the user, then displays the number of characters in that line.

Solutions:

  1. User Name and Age
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.");
  1. Multiplication Program
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);
  1. Character Count Program
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!