What is the purpose of Console.ReadLine() in C#?Console.ReadLine()
method is used to get input from the User. The input received from the
User is of type string by default. In your program, you can then cast
the result to the required type.
Here is an example to illustrate the usage of Console.ReadLine() method in C#: class sampleClass
{ Execute the above program. It will display the following line in output window: Enter an integer number: Cursor will be available in the next line. It is waiting for your input. Enter the number 1000 and click ENTER key. Now your output window will show the following line: Number you entered is: 1000 There is yet another commonly used purpose of this method. When using Visual Studio to execute your program, the output window will open, print the output and close in fraction of seconds. You could not see the output. To view output, include Console.ReadLine() as the last line in your Main() function and run the program. Now the output window will not get closed. It will display all outputs as per your program and finally the cursor will wait for your input. You can view all of them and click on ENTER key. This will close the output window.
|