C# - Your 1st Console Application - Hello World
Published: 2021-06-27 15:18:47

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/up904tizquzq6oc9rc5x.gif)

## **Understanding what you're now seeing**

The code looks liks this:

![Console App Hello World Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/611fmx7e3ly1pb1mv12x.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvk80jp9dge7jowrs84z.png)

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.WriteLine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv7242vpp4g0hhd8anl8.png)

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.

## **Outcome**:

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

![Output of Console App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw4uktwkytyipqrn9rq2.png)

## **Have a go yourself.**

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