2.5 Console Input/Output

Stasoz
3 min readApr 2, 2022

You can check this course on my website.

Console Output

We’ve printed strings and other types to the console before, but let’s remind ourselves. To work with a console (input/output) in C# there is a class — Console.

To display information, you need to call the Console.Write() class method and pass the data for output:

In example above we use a string interpolation ($ sign before string and variable in curly braces {}) to put variable into the string.

Besides string interpolation, there is another way to output variables:

Numbers in curly brackets will be replaced by the values specified at the end. The order of the variables at the end is important!

But if you need to add the jump to the next line then use — Console.WriteLine():

Console Input

Besides the output, we can get information from the console. Console.ReadLine() — allows you to get the entered string:

In the example above, the user should enter his name, the entered value is written to the variable name, then the program will greet him.

A feature of the Console.ReadLine() method is that it can read information from the console as a string.

But what if we want to enter a value for variable age as int, or height as double? Convert class will help us here, which is available by default in the .NET platform. It contains many methods for converting to different data types, let’s see some of them in practice in the example below:

Contents

Previous article -> 2.4 Type Conversion

Next article -> 2.6 Arithmetic Operatos

Join to our community in Telegram — https://t.me/itifico

If you want to support me, you can buy me a cup of coffee and I will drink it when I write the next article :)

Donations list:

  1. Dinesh Chintalapudi — 3$
  2. Unknown — 5$
  3. Neisy — 3$

--

--

Stasoz

Full Stack Developer who is inspired by new technologies