Website development and design blog, tutorials and inspiration

Your First Console Application in C#

First Console Application, Hellow World!

By , 5th November 2007 in C#

In this short tutorial, we will create a simple C# Console Application to verify that our version of Visual Studio has been installed correctly and that the .Net Framework is working properly. We will create the infamous "Hello World" application while demonstrating some important concepts of C# programming.
 

Having just installed Visual Studio, we should just test that the installation is working ok. To do this we will create a "Hello World" console application. We will introduce a few constructs that may be unfamiliar, but don't worry, we will cover these in greater detail in the upcoming tutorials.

For now, open up the Visual C# and create a new Console Application. This can be done from the File menu and select New Project, then Console Application.

In order to keep things as simple as possible for the first lesson, select all the code (Ctrl+A) or Edit » Select All, and delete it. Next, you need to type in, or copy and paste the following code into the editor.

  1. using System;
  2.  
  3. class HelloWorld
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Hello World!");
  8. }
  9. }

You should now run the program by pressing (Ctrl+F5) which will start without debugging, or from the menu select Debug » Build Without Debugging. I'll explain why in the next tutorial, but now you should see the console window that says "Hello World!"

Let's Examine the Code

On the first line, we are telling the compiler that we want to use something called System. The system is a namespace that is provided by Microsoft. Namespaces are an important concept in C# (and the .Net Framework in general) and will be covered in a future tutorial. It is similar to include statements in languages such as C/C++ and PHP.

The next line of code is creating a class called HelloWorld. Again, a class is a fundamental concept in object orientated programming and will be covered in great detail in future tutorials. For now, think of a class as a container. Everything must be contained within a class in C#, unlike C/C++ and PHP where you can have variables inside or outside a class or function.

The next line of code defines the Main function for the program. We don't need to worry too much about Main at this stage, as we will cover it in a later session. Finally, our last line of code simply tells the program to write a line onto the screen. Console.WriteLine will write a line of text to the console screen and automatically add a newline, carriage return to the end. Notice there is a semi-colon at the end of the line. C#, like a lot of programming languages, uses the semi-colon to separate lines of code or operations. Every statement will have a semi-colon at the end.

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.