Learn Code

Hello World!

Hello World!

“Hello World!” is the traditional code first written when learning a new programming language. Almost like a rite of passage. I look at it like a test to prove that you are able to communicate to the machine, and it returns a message that we’ve asked for to confirm that we have successfully given it commands. Like a blink-once-if-you-understand-me situation.

The goal and intent of this blog series is to share what I have learned about programming, hopefully in a manner that makes sense. To start we will follow the tradition and create our own “Hello World!” application. Including how to set up a coding environment, understand the syntax we will use for the “Hello World!” application, and how to compile the code and have it display on screen.

Download Visual Studio 2019 Community Edition

For our series, we will be using Visual Studio 2019 Community. You can download it here. It is the free version from Microsoft. Just click the “Free Download” button under the “Community” section.

Download the FREE Visual Studio 2019 Community

Clicking the download button will add an installer to your computer. Double-clicking that file, may prompt you with a security warning. It is asking you to confirm that you want to run this file. Confirm that you want to install it and follow the prompts to load it on your computer. While that is installing, let’s talk a bit more about Visual Studio…

It may take some time to install… start it up and come back!

Visual Studio is an integrated development environment (IDE), meaning it is designed specifically to help programmers work more efficiently. Code can be written in a lot of different applications, even Notepad. But having a fully featured IDE, includes having additional tools that makes programming easier.

One such tool is a compiler, we won’t go too deep into what all a compiler does right now, but I’ll try to give a good analogy: Let’s say you are trying to communicate with someone that does not speak the same language as you. There are some very detailed and specific instructions that you need them to have. How would you give them the instructions? One way would be for you to learn the other persons language so proficiently that you can translate the instructions while maintaining all the nuances and meanings that are common and familiar in your own language. Another way would be to find a translator that understands both your language and the other persons language fully so that they can make sure that all the intended meanings are conveyed to the other person.

That is part of what a compiler does. It knows the code (instructions) that you give it. And it knows the language of the computers processor. It takes your instructions, compiles them as efficiently as it can into instructions that the computer can understand and do something with. That’s one less thing you have to try to do, which is awesome! You can focus on adding the features and aspects that you are trying to program with less need to concern yourself with how you’re going to get the computer to understand what you want it to do.

Once Visual Studio has finished installing, let’s go ahead and open it and start creating amazing-ness! If you see the screen below, click Launch to start it up. If not you should be able to find it in your Start menu.

Install Visual Studio 2019 – Open it for the first time!

When you first start Visual Studio you should see a screen like this:

Let’s start with the “Create a new project” in the lower right corner.

We will select Console App (.NET Core) and then click Next in the lower right corner – don’t worry if that selection sounds technical or confusing right now, we will cover more about what all that is in time. For now, just think of it as an application that is all text-based.

Select Console App (.NET Core) then click Next

Next it will ask you to name your project, and tell it where you want to save the files that it will create for you.

We will name our project HelloWorld

Nope, that is not a typo, we want to put the words right next to each other. We capitalize the first letter of each word to help distinguish where the words break. We want to make sure the checkbox is selected to “Place solution and project in the same directory”. and then click “Create”. It will take a couple moments while it creates all the files it needs, and then…

Ta-Da!

Visual Studio finishes creating the project and presents it to you. If you have not worked with Visual Studio or some other IDE before, it could be very overwhelming to see all the windows and menu tabs and icons everywhere. But try not to worry about it right now, learning new things takes time and there is no limit saying you have to know it all right now or even 5 years from now.

So as I mentioned at the beginning “Hello World!” is pretty common. It is so common and anticipated that Visual Studio actually puts all the code required in the starter project for you. To run the program you can click the HelloWorld next to the green arrow at the top, or simply press F5 on your keyboard.

That will open the application in a “Console” window like this:

There at the very top we see the expected output. “Hello World!” Congratulations, you have run your first program! But what about all the stuff under our expected output? That was not expected. What does it mean?

Running the application this way runs it in debug mode. There is another way to run, but it still returns more information than we expect. The way to run that is to either to select “Debug” from the top menu, then “Start without Debugging” or to use the keyboard shortcut by pressing and holding the “Ctrl” key plus “F5”. (Often seen as “Ctrl+F5”)

But as was mentioned it still gives us information about the process exited with code 0… what does that mean? Basically, it means there were not any errors encountered when it ran our code. But if we want to get rid of that line as well we can do what the first message stated:

“To automatically close the console when debugging stops, enable Tools > Options > Debugging > Automatically close the console when debugging stops.”

  • So we will click “Tools” from the menu bar.
  • Select “Options…”
  • This will bring us to a page like this, we can scroll down on the left (or close the Environment arrow by clicking it) until we find “Debugging”
  • Select “Debugging”
  • Scroll all the way to the bottom of the right side of the window and you should see the checkbox for “Automatically close the console when debugging stops”
Click the checkbox if you don’t want to see the debug information in the console.

The challenge now is that if we run our program (F5 or Ctrl+F5) it runs, but seemingly instant it closes so we do not see our output.

Let’s add a line of code to the program and run it again. Currently our code looks like this:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

We will add a new line of code right after the line “Console.WriteLine(“Hello World!”); Like this:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}

What does that do?

As you likely guessed the line “Console.WriteLine(“Hello World”);” is an instruction for the computer to do something. Specifically, it is telling the console to write the line “Hello World!”

The line we added is telling the computer that we want the program to wait for some input from the person. We are telling it to read the line. So it waits until the Enter key is pressed. That indicates to the program that the next step is to read what was typed before the Enter. You could type something there and press Enter. At this point it would not do anything with your input, because we did not tell it to. So the program is reading one line at a time and sees that the last thing it has to do is read a line. After it reads, it does what we told it to do when we clicked the checkbox. It automatically closes the console because there is nothing left for it to debug (or read through).

Where do we go from here?

Well, the sky is pretty much the limit. There is a ton that can be learned about programming. Personally I think it is an exciting and fun thing to learn. And it continues to grow and expand, so there is always more to learn. But from the aspect of this blog, and specifically this series, I am going to work through C# and try to teach and share what I can so that you can gain the confidence and skills to do what you want with programming. Maybe for some that means starting a new career or maybe it’s just a hobby. In any case, I hope that you will continue to follow this series, as well as check out my other series. And I hope that you gain as much from this as I do in putting it all together.

Next up: Let’s build another classic program in a C# console application: a To-Do list.