What is the console?

In C#, the console refers to the standard input and output streams of a command-line interface or a terminal window. The console provides a way to interact with the user and display information directly in text form. It's commonly used for debugging, logging, and simple text-based user interfaces.

Different ways to write to the console

In order to write a line of text to the console, this line of code is used:

Console.WriteLine("Hello, World!");

The output would actually be:

Hello, World!
                            

As you can see, there is a new line created. WriteLine appends a new line to the end of whatever text you are printing to the console. In order to prevent a new line from being created, you can use Write.

Console.Write("Hello, World!");

The output would be:

Hello, World!

As you can see, there is no new line appended. Forewarning, if you do try this out in your own program, you won't see the new line. However, you can see the difference if you run these two code blocks.

Console.Write("Hello, World!");
Console.WriteLine("Hello, World!");
Console.WriteLine("Hello, World!");
Console.Write("Hello, World!");

User Input

There are two main ways of getting user input.

Console.ReadKey()
Console.ReadLine()

ReadKey is used to detect user keyboard input. ReadLine is used to return whatever the user typed in. For example:

Console.Write("What is your name: ");
string name = Console.ReadLine();

The console would return.

What is your name: 

I would type in my name and it would look something like this.

What is your name: Rohit

And now the name variable has the value of: Rohit

We will implement user input in our bank account simulation in the next section!

Next Section: Operators