DEV Community

Cover image for C# - Your 1st Console Application - Hello World

Posted on

C# - Your 1st Console Application - Hello World

Ok, so you may have heard the famous "Hello World" application. This is usually the first application people learn when learning any new language. Mainly, its due to its simplicity.

Getting ready

  1. First of all load up the Visual Studio IDE.
  2. From the startup window select "Create a new project"
  3. Search for Templates
  4. Type "_Console App (.Net Core)" and select
  5. Enter a project name of "Hello World"
  6. Click "Create";

create console app demo

Understanding what you're now seeing

The code looks liks this:

Console App Hello World Code

so again lets disect this:

"Using" statements

You will often see these statements on any code you download or view online. The "using" keyword is used in C# to import other code into your file.

.Net Core / .Net Framework is made up of multiple code packages. You can utilise these by writing "using" and the name of the namespace you wish to import / utilise.

What's a namespace ?

A namespace is a way of categorising or grouping your class files and code. Each class file should be given a namespace. You can then use this namespace to import these files into other projects and files.

In the example above you can see the app has automatically created the "Hello_World" namespace. You can call your namespaces whatever you want, but try to stick with a pattern. I normally go with something like
{ProjectName}.{Area} e.g "CalculatorApp.Multiplication" or something.

"class Program"

This defines the class name, in which the console app is going to run. It

static void Main(string[] args)

static void main

So we've hit something new.. the "static" keyword. This keyword means that in order to use the Method, you don't have to create a new instance of the class, you could simply call Program.Main() if you wanted to.

"void" -- Like we've seen in other methods, the second part of a method declaration is the return type. By using the word "void" it tells the application that there will be no return type from this method, i.e it doesn't return anything.

"Main" -- This is the entry point of C# console applications. When you run the console application Main is the first method that will be ran. A C# application can only have 1 entry point Main method.

string[] args -- the Main method can be declared with or without command line arguments. You can enter parameters to a console application, which can then be used within the application (but you don't need to worry about that just right now).

Console.WriteLine('Hello World')


Console is the static class, and WriteLine is a static method, meaning we can call it without having to create an instance of the class; like you saw in the previous tutorial where I created a new instance of User (var user = new User());

We pass in the string paramater "Hello World". This is the text we wish the program (console app) to display within the console when ran.

Lets see it in action; there are two ways we can do this.

Option 1: At the top of the toolbar within Visual Studio there is a big green triangle. Click this "Run" button, and your application will start and run.

Option 2: Press the "F5" key this also run the application.


You should now see a command prompt console and it should display "Hello World".

Output of Console App

Have a go yourself.

Try changing the text that is inside the Console.WriteLine() method, and make it display something else.

Discussion (0)