C# User Input

Table of Contents

  • What is C# User Input?
  • How to take input in C#
  • Readline()
  • Read() in C#
  • ReadKey()
  • References
  • Conclusion

What is C# User Input?

As we have discussed in our How to take C# Output chapter that how to print on console using Console.WriteLine() method/function which is used to print any thing we want on our screen/console. Today we discuss in this chapter that how can we take C# User Input i.e(taking input of a number , text , character etc) using Console.ReadLine() method/function.

C# User Input
C# User Input

How to take input in C#

In the Below example, the user can input his or hers name, which is stored in a variable namely Name. Then we print the value of Name variable:

using System;

namespace Codelikechamp
{
  class Program
  {
    static void Main(string[] args)
    {
      // Type your username and press enter
      Console.WriteLine("Enter username:");

      // It's Create a string variable and get user input from the keyboard and store it in the variable
      string Name = Console.ReadLine();

      // Print the value of the variable (Name), which will display the input value
      Console.WriteLine("Username is: " + Name);
    }
  }
}

Output:

Enter username:
usman
Username is: usman

NOTE: Console.ReadLine() methods return a string value so you can only use it when you want to get a text type input from user . You can’t use it for getting other type of data this will show you error! :

Console.WriteLine("Enter your age:");
int age = Console.ReadLine();
Console.WriteLine("Your age is: " + age);

Output:

Cannot implicitly convert type 'string' to 'int'

This will show you error as you seen above so to resolve this error we do conversion of data type / type casting which we have studied in previous chapter of (Type Casting in C#). Look below same example but with conversion we get our desired output:

Console.WriteLine("Enter your age:");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is: " + age);

Output:

Enter your age:
30
Your age is: 30

ReadLine()

  • ReadLine() method in C# is like listening for what a user types on the computer.
  • It waits until the user presses “Enter” button and then gives you what they typed as a text.
  • You can use this text in your program for various purposes, like asking for their name and then saying “Hello” with it as we have done it in above example.
  • It always return a string value
  • so when you need to get data in different datatype so you have to do type casting.

Read() in C#

  • Console.Read() in C# reads a single character from the user’s input.
  • It returns the numeric code of the pressed key.
  • Useful for scenarios where you need to respond to specific key presses.

ReadKey()

  • Console.ReadKey() in C# reads a single key press from the user.
  • It returns information about the pressed key, such as the character and key code.
  • Useful for interactive console applications where you need to respond to individual key presses.

References

  1. Microsoft C# Documentation:
  2. GeeksforGeeks:
  3. C# Corner:
    • A community-driven platform with tutorials and articles on C# development. You can find various articles related to user input handling: C# Corner

Conclusion

In last i want to conclude that, user input is a fundamental aspect of interactive C# console applications. The Console.ReadLine() method allows your program to read input from the user, which can then be processed and used in your application. You can use various parsing and validation techniques to ensure that the input meets your program’s requirements.

Website Link: https://Codelikechamp.com

Medium Link: Follow me on Medium

Linkedin Link: Follow me on Linkedin

Youtube channel Link: Subscribe my Channel

Facebook Group: Join our Facebook Group

🤞 Don’t miss any latest posts!

Please subscribe by joining our community for free and stay updated!!!

IF YOU HAVE ALREADY SUBSCRIBED JUST CLOSE THIS FORM !

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top