How to Take Input in C# | #MortalTech | #3



How to Take Input in C# | #MortalTech | #3

How to Take Input in C# | #MortalTech | #3

How to Take Input in C# | #MortalTech | #3

C# Input
In C#, the simplest method to get input from the user is by using the ReadLine() method of the Console class. However, Read() and ReadKey() are also available for getting input from the user. They are also included in Console class.

Example 6: Get String Input From User
using System;

namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string testString;
Console.Write(“Enter a string – “);
testString = Console.ReadLine();
Console.WriteLine(“You entered ‘{0}'”, testString);
}
}
}
When we run the program, the output will be:

Enter a string – Hello World
You entered ‘Hello World’
Difference between ReadLine(), Read() and ReadKey() method:
The difference between ReadLine(), Read() and ReadKey() method is:

ReadLine(): The ReadLine() method reads the next line of input from the standard input stream. It returns the same string.
Read(): The Read() method reads the next character from the standard input stream. It returns the ascii value of the character.
ReadKey(): The ReadKey() method obtains the next key pressed by user. This method is usually used to hold the screen until user press a key.
…………………………………….
You can also find me here:
Instagram: MortalsTech
Facebook: MortalTech
Twitter: Mortalstech