In C#, User Input allows a program to receive data from the user during runtime. This makes programs interactive and dynamic.
The most common way to take user input in C# is by using the Console.ReadLine() method.
The Console.ReadLine() method reads input as a string from the keyboard.
// Basic User Input Example
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
Since user input is always read as a string, it must be converted to other data types when needed.
| Method | Description | Example |
|---|---|---|
| int.Parse() | Converts string to int | int age = int.Parse(Console.ReadLine()); |
| Convert.ToDouble() | Converts string to double | double price = Convert.ToDouble(Console.ReadLine()); |
| Convert.ToBoolean() | Converts string to bool | bool isActive = Convert.ToBoolean(Console.ReadLine()); |
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());
Console.WriteLine("Your age is: " + age);
User input is a fundamental concept in C# programming. Proper handling and conversion of input helps in building robust and user-friendly applications.