Your First C# Application Tutorial

In this tutorial we learn how to write, compile, and run a basic C# application. We also provide a quick breakdown of this basic application to understand more easily how it works.

New Project

Now that we have our environment set up, let’s create our very first C# Application.

  1. Start Visual Studio.
  2. On the menu bar, choose File > New > Project.
  3. In the left pane of the dialog box, expand the Installed option and click on Visual C#.
  4. In the middle pane, choose Console App.

Console applications are great for learning a language because we don’t have to worry about other complexities, such as a user interface.

  • Name: Specify the project name as HelloWorld.
  • Location: Choose a location on your hard drive for the files to be stored.
  • Solution Name: Specify the solution name as HelloWorldApp.
  • Framework: Set the Framework to the highest number.

Click OK to create a new project in Solution Explorer.

In Visual Studio we have the concept of a Solution, which can hold one or more projects. The solution name can be different than the project name.

Visual Studio

Most new developers are quickly intimidated when they start with Visual Studio. In truth, 90% of the time you’ll only be using 10% of what you see.

We only need to focus on three windows:

  • Code Editor, where you type all your code
  • Solution Explorer, which shows you the structure of your solution
  • Output Console, where we see our errors

And one button:

  • Start, which runs our application

We also have two shortcuts for Start

  • F5 - Starts with debugging
  • Ctrl+F5 (Cmd+F5 on Mac) - Starts without debugging

Hello World

If your file is not open in the Code Editor, double-click on the Program.cs file under HelloWorld in the Solution Explorer to open it.

You should see the following:

Default:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

By default, when you create a new solution or class, Visual Studio will automatically add this code with whichever class name you choose.

For now, you can delete everything and type in the following code:

First App:
using System;

// My first C# program
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "Hello World";

            Console.WriteLine(message);
            Console.ReadLine();
        }
    }
}

That’s it, you’ve just created your first C# application. Congratulations!

Now, let’s run the application and then break it down step by step so that you understand what’s happening.

Run your first application

Click the Start button to run the application or use the shortcuts (Ctrl+F5 / Cmd+F5).

A Console window will appear that contains the text “Hello World”.

Note that your anti-virus application may want to scan the application before it runs. You can temporarily disable this feature, however it may put your system at risk to other threats.

Anatomy of your first application

Throughout the tutorial series we’ll do more in-depth tutorials on each of the following subjects.

For now though, let’s do a short breakdown of the application we just made and what the code means.

Directives

Namespaces are used to group classes together. The using directive is used to access everything inside that namespace.

Example:
 using System;

In this case the System namespace includes fundamental classes which define commonly used data types such as bool, array, events, interfaces etc.

Note that we use a semicolon to end the statement.

Comments

C# allows us to document our code, which we do with comments.

Example:
using System;

// My first C# program

One of the ways we write comments is to prefix text on one line with two forward slashes ( // ).

Note that comments are ignored by the compiler.

Namespaces

The namespace HelloWorld will group our classes.

Example:
using System;

// My first C# program
namespace HelloWorld
{

}

By default, Visual Studio will name the namespace the same as your project name, but it’s allowed to by something else.

Note that we use open and close curly braces. Everything inside the curly braces will be in the namespace.

Classes

Inside the namespace we indent once and declare a class called Program.

Example:
using System;

// My first C# program
namespace HelloWorld
{
    class Program
    {

    }
}

A class will also group sections of code and is used to model our application after entities in the real world.

By default, Visual Studio will name the class Program when you start a new project, but the name is allowed to be something else.

Note that we use open and close curly braces again. Everything inside the curly braces will be in the class.

Functions / Methods

Inside the class we indent once and define a function, called Main.

Example:
using System;

// My first C# program
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}

A function is one or more code statements combined into a group to allow code to be reusable.

Note that the Main() function is required once in any executable C# application and acts as a starting point for our code.

Variables

Inside the Main() function we declare a variable called message with the text “Hello World”.

Example:
using System;

// My first C# program
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "Hello World";
        }
    }
}

A variable is simply a data container to hold things like numbers, text, etc.

Function / Method calls

Inside the Main() function, we do what’s referred to as a function call.

Example:
using System;

// My first C# program
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "Hello World";

            Console.WriteLine(message);
            Console.ReadLine();
        }
    }
}

The two functions will do the following:

  • Console.WriteLine() will output the text inside the message variable to the console window.
  • Console.ReadLine() will stop the console window from immediately closing after running.

The System namespace makes available to us the Console class, which contains a function called WriteLine. The WriteLine() function’s responsibility is to correctly output text to the console window.

Note that function calls don’t have curly braces but a semicolon instead. That’s because we’re calling (using) the functions here, instead of defining (creating) them.