Using variables and learning about the Console using C#, .net and Visual
Studio 2019

Welcome to another Basic C# tutorial!

In my Last tutorial blog post I walked you through how to create a basic console app and print out the string hello world to the console. This post taught you a little bit about variables and using Visual Studio to create a project.

In this article you will find a basic C# tutorial. We will first add a name variable to our project then prompt the user to input their name, then ask the Console to say “Hello <name>” instead our our generic Hello world.

01

Open the “Hello World” project that we created last time or download it from my GitHub.

Double click on “Hello World.cs” to open our code file in the editor.

Move your cursor into the “Main” method  and type the following: “string name;”

//using statements import libraries of code for us to use in our project in this case we are importing "System" which contains the definition for the Console methods
using System;
//our namspace is used to organize your code they can be named whatever you want
namespace Hello_World
{
    class HelloWorld
    {
        
        //A console app mist always have a Main method
        static void Main(string[] args)
        {

            //declare a variable to hold the name that we are about to input
            string name; 
            //call the Console and tell it to print the words "Hello World"
            Console.WriteLine("Hello World!");
        }
    }
}

02

Next we will tell the Console to prompt the user for input by asking the user to input their name.

Move your cursor inside the “Main” method and and type the line that tells the Console to request the user inputs a name by typing “Console.WriteLine(“Please input your name: ” );  (an important thing to note here is that when accessing the methods of a library object our text is case sensitive.  So if you type “Console.writeline” you will get an error.)
Now we need to tell the program that it is expecting the user to input text. We do that by typing “Console.ReadLine()”. This will show the user a flashing cursor and will wait for the user to input something before continuing.

//using statements import libraries of code for us to use in our project in this case we are importing "System" which contains the definition for the Console methods
using System;
//our namspace is used to organize your code they can be named whatever you want
namespace Hello_World
{
    class HelloWorld
    {
        //declare a variable to hold the name that we are about to input
        string name; 
        //A console app mist always have a Main method
        static void Main(string[] args)
        {
            //Print out text to request the name 
            Console.WriteLine("Please input your name: ");

            //next tell the Console to read the input
            Console.ReadLine();
                       
            //call the Console and tell it to print the words "Hello <name>"
            Console.WriteLine("Hello World!");
        }
    }
}

03

Next we will change the line that used to tell us “Hello world” to use our Variable “name”.

within the “Main” method find the line that says Console.WriteLine(“Hello World!”); and change it to the following :

“Console.WriteLine(“Hello ” + name + “!” );”

//using statements import libraries of code for us to use in our project in this case we are importing "System" which contains the definition for the Console methods
using System;
//our namspace is used to organize your code they can be named whatever you want
namespace Hello_World
{
    class HelloWorld
    {
      
        
        //A console app mist always have a Main method
        static void Main(string[] args)
        {
            //declare a variable to hold the name that we are about to input
            string name;
            //Print out text to request the name 
            Console.WriteLine("Please input your name: ");

            //next tell the Console to read the input
            name =  Console.ReadLine();
                       
            //call the Console and tell it to print the words "Hello <name>"
            Console.WriteLine("Hello " + name + "!" );
        }
    }
}

 

04

Run the program using the play button at the top or by pressing “F5”

Hey you did it! If you would like to learn a little more about the concepts here you can look at my article explaining variables.
or if you feel like you’re ready for more coding continue on to another basic C# tutorial.

 

Follow me on Social Media!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.