Input and Output via Keyboard and Screen

In this tutorial, we will see both how to input information into our application and how to display it.

 

1 - Keyboard Input

To enter information via the keyboard, all we need to do is write the following line of code

Console.ReadLine();

When we do this, if we press the play button in Visual Studio, it will allow us to enter any value until we press enter.

However, if we only receive the information, we’re not doing anything with it, so we need to store it in a variable

String etradaTeclado = Console.ReadLine();

 

 

2 - Output to Screen

Displaying information is very simple, we just need to use the statement Console.WriteLine(variable)

Therefore, we can make an example that asks the user for their name and displays it on the screen.

Console.WriteLine("Introudce tu nombre:");
string nombre = Console.ReadLine();
Console.WriteLine($"El nombre es {nombre}");
Console.ReadKey();

The code piece we see to display in Console.WriteLine() is called string interpolation which we will see further on in this course.

Finally, we put a Console.ReadKey() so that the code execution pauses until we enter a single character via the keyboard

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café