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 {
public static void Main() {
Console.WriteLine(“Enter an integer number:”);
int sampleNo = int.parse(Console.ReadLine());
Console.WriteLine(“Number you entered is: ”+sampleNo);
}
}

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.

| What is the need for Bridge Pattern in C#? | What is the need for Builder Pattern in C#? | What is the need for Chain of Responsibility Pattern in C#? | What is the need for Command Pattern in C#? | What is the need for Composite Pattern in C#? | What is the need for Decorator Pattern in C#? | What is the need for Flyweight Pattern in C#? | What is the need for Interpreter Pattern in C#? | What is the need for Iterator Pattern in C#? | What is the need for Mediator Pattern in C#? | What is the need for Memento Pattern in C#? | What is the need for Prototype Pattern in C#? | What is the need for State Pattern in C#? | What is the need for Strategy Pattern in C#? | What is the need for Template Method Pattern in C#? | What is the need for unsafe code in C#? | What is the purpose of assert() in C#? | What is the purpose of AutoResetEvent in .NET? | What is the purpose of Console.ReadLine() in C#? | What is the purpose of machine.config file in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.