Console.ReadLine() Method in C#

The Console.ReadLine() method in C# is used to read the next line of characters from the standard input stream. It reads input from the user until the Enter key is pressed and returns the input as a string.

This method is commonly used in console applications to capture user input for processing. It waits for user input and returns null if the input stream has been redirected and no more lines are available.

Syntax

Following is the syntax for the Console.ReadLine() method −

public static string ReadLine();

Return Value

The method returns a string containing the next line of characters from the input stream, or null if no more lines are available.

Using Console.ReadLine() for String Input

Since online compilers cannot accept user input, we'll demonstrate with predefined values −

using System;

public class Demo {
   public static void Main() {
      string subject = "Mathematics";
      string studentName = "John Smith";
      
      Console.WriteLine("Subject: " + subject);
      Console.WriteLine("Student: " + studentName);
      Console.WriteLine("Input processing completed successfully!");
   }
}

The output of the above code is −

Subject: Mathematics
Student: John Smith
Input processing completed successfully!

Using Console.ReadLine() with Type Conversion

When reading numeric input, Console.ReadLine() returns a string that needs to be converted to the appropriate numeric type −

using System;

public class NumberInput {
   public static void Main() {
      // Simulating user input
      string ageInput = "25";
      string salaryInput = "50000.75";
      
      int age = Convert.ToInt32(ageInput);
      double salary = Convert.ToDouble(salaryInput);
      
      Console.WriteLine("Age: " + age);
      Console.WriteLine("Salary: $" + salary);
      Console.WriteLine("Next year age will be: " + (age + 1));
   }
}

The output of the above code is −

Age: 25
Salary: $50000.75
Next year age will be: 26

Handling Multiple Lines of Input

You can read multiple lines of input by calling Console.ReadLine() multiple times −

using System;

public class MultipleInputs {
   public static void Main() {
      // Simulating multiple user inputs
      string firstName = "Alice";
      string lastName = "Johnson";
      string city = "New York";
      
      Console.WriteLine("Personal Information:");
      Console.WriteLine("First Name: " + firstName);
      Console.WriteLine("Last Name: " + lastName);
      Console.WriteLine("City: " + city);
      Console.WriteLine("Full Name: " + firstName + " " + lastName);
   }
}

The output of the above code is −

Personal Information:
First Name: Alice
Last Name: Johnson
City: New York
Full Name: Alice Johnson

Key Points

  • Console.ReadLine() always returns a string, even for numeric input.

  • The method waits for the user to press Enter before returning the input.

  • For numeric operations, convert the string using Convert.ToInt32(), Convert.ToDouble(), etc.

  • Returns null if the end of the input stream is reached.

Conclusion

The Console.ReadLine() method is essential for reading user input in console applications. It returns a string that can be processed directly or converted to other data types as needed for your application's logic.

Updated on: 2026-03-17T07:04:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements