0
0
C Sharp (C#)programming~5 mins

Console.ReadLine for input in C Sharp (C#)

Choose your learning style9 modes available
Introduction

We use Console.ReadLine to let the user type something on the keyboard. It helps the program get information from the person running it.

When you want to ask the user for their name.
When you need the user to enter a number or text to decide what to do next.
When you want to pause the program and wait for the user to press Enter.
When you want to get input like a password or a message from the user.
When you want to read multiple lines of text from the user.
Syntax
C Sharp (C#)
string input = Console.ReadLine();

Console.ReadLine() reads everything the user types until they press Enter.

The result is always a string. You may need to convert it if you want numbers.

Examples
This reads a line of text and stores it in the variable name.
C Sharp (C#)
string name = Console.ReadLine();
This asks the user to enter their age and reads it as a string.
C Sharp (C#)
Console.WriteLine("Enter your age:");
string ageInput = Console.ReadLine();
This reads a line and converts it to an integer number.
C Sharp (C#)
int age = int.Parse(Console.ReadLine());
Sample Program

This program asks the user for their name, reads it using Console.ReadLine(), and then greets them by name.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("What is your name?");
        string name = Console.ReadLine();
        Console.WriteLine($"Hello, {name}!");
    }
}
OutputSuccess
Important Notes

If you want to read numbers, remember to convert the string using methods like int.Parse() or int.TryParse().

Console.ReadLine() waits until the user presses Enter, so the program pauses there.

Summary

Console.ReadLine() reads user input as a string from the keyboard.

It is useful to get information from the user during the program.

Always remember to convert the input if you need a number or other type.