C# console reading values



 This C# program reads a value from a console and prints it.

C# console reading values

In this example, we use the Console class to read values.
using System;
using System.Text;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
     	Console.Write("Enter your name: ");
        string name = Console.ReadLine();
        Console.WriteLine($"Hello {name}"); 
     }
  }
}

Output:

Enter your name: Admin

Hello Admin

We read a line from the terminal. When we hit the Enter key, the input is assigned to the name variable. The input is stored into the name variable, which is declared to be of type string.

string name = Console.ReadLine();

In this code line, we do string formatting. The {name} specifier is replaced with the value of the name variable.

Console.WriteLine($"Hello {name}"); 

Comments