Input: Taking data from the user while the program is running. Output: Displaying results, messages, or information to the user. In C#, user interaction is done using the Console class from the System namespace. Below is an example:
Console
System
Read data using Console.ReadLine() or Console.ReadKey().
Console.ReadLine()
Console.ReadKey()
Display data using Console.Write() or Console.WriteLine().
Console.Write()
Console.WriteLine()
Programs become interactive instead of hardcoded, making them more flexible for users.
User input can be used for calculations, validations, and decision-making processes.
From command-line tools to large applications, I/O is essential for real-world solutions.
These methods help in taking input from users and displaying output on the console.
Console.Read()
using System; class Program { static void Main() { // Using Write() Console.Write("Hello "); Console.Write("World"); // Output: Hello World (same line) // Using WriteLine() Console.WriteLine(); Console.WriteLine("Welcome to C# programming!"); // Output: Moves to next line automatically } }
Explanation: - Console.Write() prints text without moving to a new line. - Console.WriteLine() prints text and moves to the next line automatically.
Expected Output:
Hello World Welcome to C# programming!
using System; class Program { static void Main() { // Input using ReadLine() Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.WriteLine("Hello, " + name + "!"); // Input using ReadKey() Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } }
Explanation: - Console.ReadLine() reads a full line of text input from the user. - Console.ReadKey() waits for a single key press and does not require Enter.
Example Interaction / Output:
Enter your name: Megha Hello, Megha! Press any key to continue...
using System; class Program { static void Main() { Console.Write("Enter your age: "); int age = Convert.ToInt32(Console.ReadLine()); // Convert string to int Console.WriteLine("Next year, you will be " + (age + 1)); } }
Explanation: - Input from Console.ReadLine() is always a string. - Convert.ToInt32() converts the string input into an integer for numeric operations.
Convert.ToInt32()
Enter your age: 25 Next year, you will be 26
Common Conversion Methods: Convert.ToInt32(), Convert.ToDouble(), Convert.ToBoolean(), int.Parse(), double.Parse()
using System; class Program { static void Main() { Console.Write("Enter a number: "); if (int.TryParse(Console.ReadLine(), out int number)) { Console.WriteLine("You entered: " + number); } else { Console.WriteLine("Invalid input! Please enter a number."); } } }
Explanation: - int.TryParse() safely converts a string to an integer. - If the input is invalid, the program does not crash and instead provides an error message.
int.TryParse()
Enter a number: 42 You entered: 42 Enter a number: abc Invalid input! Please enter a number.
Program: ProductInputDemo.cs
using System; class Program { static void Main() { Console.Write("Enter product name: "); string product = Console.ReadLine(); Console.Write("Enter price: "); double price = Convert.ToDouble(Console.ReadLine()); Console.WriteLine($"Product: {product}, Price: {price:C}"); } }
Enter product name: Laptop Enter price: 45000 Product: Laptop, Price: ₹45,000.00
This program demonstrates user input and output in C#:
:C