C# User Input Example

In this source code example, we demonstrate how to read values from the console or how to get user input.

We use the Console class to read values.

C# User Input Example

The below C# program reads a value from a console and prints it:

using System;

class Program {
  static void Main() {
    Console.Write("Enter your first name: ");
    string firstName = Console.ReadLine();
    
    Console.Write("Enter your last name: ");
    string lastName = Console.ReadLine();
    
    Console.Write("Enter your age: ");
    string age = Console.ReadLine();
    int intAge = Convert.ToInt32(age);
    
    Console.WriteLine($" First name : {firstName}");
    Console.WriteLine($" Last name : {lastName}");
    Console.WriteLine($" Age : {intAge}");
  }
}

Output:

Enter your first name: Ramesh
Enter your last name: Fadatare
Enter your age: 30
 First name : Ramesh
 Last name : Fadatare
 Age : 30
The {firstName}, {lastName} and {intAge} specifier is replaced with the value of the firstName, lastName and intAge variable.

Comments