Getting Input From Users

Lesson 8
Author : Afrixi
Last Updated : February, 2023
C# - Programming Language
This course covers the basics of programming in C#.

In C#, you can get input from users through the console using the Console.ReadLine() method. This method reads a line of input entered by the user and returns it as a string. Here’s an example:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What is your name?");
            string name = Console.ReadLine();
            Console.WriteLine("Hello, " + name + "!");

            Console.ReadKey();
        }
    }
}

In this example, we’re prompting the user to enter their name using Console.WriteLine(). Then, we’re using Console.ReadLine() to read the input and store it in a variable called name. Finally, we’re using Console.WriteLine() to output a personalized greeting using the user’s input.

Note that the Console.ReadKey() method is used at the end to prevent the console from closing immediately, allowing the user to see the output of the program.