How to read inputs as strings in C#?


To read inputs as strings in C#, use the Console.ReadLine() method.

str = Console.ReadLine();

The above will read input as string. You don’t need to use the Convert method here since the input takes string by default.

Now display the string entered by user −

Example

 Live Demo

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string myStr;

      // use ReadLine() to read the entered line
      myStr = Console.ReadLine();

      // display the line
      Console.WriteLine("Result = {0}", myStr);
   }
}

Output

Result =

The following is the output. Let’s say the user entered “Amit” as the input −

Result = amit

Updated on: 22-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements