First application in C#

October9, 2017
by admin

Let’s now start by creating a first C# application and understand the program structure of C#. It is most common to create a simple ’Hello World!’ application when you start learning any language. So, let’s do the same here.

If you don’t want to download visual studio, then you can download the latest .Net framework that is compatible with your computer operating system configuration. Do read system requirements first while downloading .Net framework. If your configuration matches with the latest version go ahead and download it. If it doesn’t match with your system configuration, then try previous versions of the .Net framework. Once you have installed the .net framework, you are ready to start learning. You can code within any editor such as Notepad, Notepad++ or any other third party editor. Write the program in a text editor. Save it with .CS extension and then compile and run with CSC.exe that comes with .net framework.

This course assumes that you have at least downloaded express edition of visual studio so that you will get more benefits such as intellisense, debugging and error handling etc. Lets start creating ’hello world’ console application.

To create a C# application, open visual studio express.

Click menu item ’File > New Project’.

In a dialog box, select ’Windows > C#’ template from left hand side pane.

Then click ’Console application’.

Type name of the application in the text box. Let’s name it as ’HelloWorldApp’.

Browse to the path of the folder where you want to save the application.

Let be ’create solution folder’ check box checked.

Click Ok.

This will result in creating a new console application named ’HelloWorldApp’.

You will see the editor window having the default file ’Program.cs’ open.

You will see the following code in the editor.


  using System;

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

      }  
    }
  }

Lets understand this code first.

On the first line of code editor you will see a statement with ’using’ statement as follows.

using System;

Here ’using’ is an in-build keyword and ’System’ is the namespace from the .net library.

So what is a namespace?

As you know .net is an object oriented programming. So .net base class library consists of lots of useful classes. A Namespace is used for grouping these classes. Some common classes are grouped into ’System’ namespace. Likewise there are many other namespaces in the base class library which forms groups of related classes. If we want to refer or use  any of the .net class, then we have to first import the namespace belonging to that class.

In C# we are importing namespaces using ’using’ keyword at the top of the code editor. Once we have imported a required namespaces at the top of the code editor then we can use any classes within those namespaces.

On the next line of code there is a namespace defined.

namespace HelloWorldApp

This is because the class ’Program’ belongs to this namespace. So the class is wrapped within the namespace using {} curly braces.

Note that the name of the namespace ’HelloWorldApp’ is our application name.

Inside the namespace there is a class definition named ’Program’ having body defined with the curly braces.

class Program{

}

We will discuss more about classes later in this course.

Within the ’Program’ class there is a single method ’Main()’.

static void Main(string[] args)
{

}

Lets understand this function signature. It starts with the ’static’ keyword. It indicates to the compiler that the ’Main()’ function can be invoked without creating the instance of the ’Program’ class.

In order to use any class within an object oriented programming we need to create an object of a class. This is also known as an instantiation of a class.

The only way we can access the members defined within the class is by creating an object of that class. But when the method is a ’static’ method, then there is no need to create an object of a class in order to invoke that method.

The next keyword is the ’void’. It tells the compiler that this method doesn’t return anything. More about return type will be discussed later.

Then there is a method named ’Main’ having a single argument of the string data type array. We will learn about data types, variable and arrays later in this course.

The body of the Main method is the place where we are going to write our code.

Remember the ’Main()’ method is the starting point of the application. Whenever you compile and run the application, this method will be invoked first.

Write the following code within the body of Main method.

Console.WriteLine(“Hello World!”);
Console.ReadLine();

In the first line we have used ’WriteLine()’ method of the ’Console’ class, passing the string “Hello World!” as an argument . This function will print the passed string to console output.

Note that ’Console’ is the class resides within the ’System’ namespace. Since we have already imported ’System’ namespace at the top of the editor, we can use the ’Console’ class.

If we don’t import ’System’ namespace before using the ’Console’ class, then we have to use fully qualified class name as follows.

System.Console.WriteLine(“Hello World!”);

Also note that we haven’t created an object of the ’Console’ class. That is because the ’WriteLine()’ is a static method. So we can simply access it by using the class name and then immediate dot(.) to access it.

This statement will print a string ’Hello World!“ to the console output window.

On the next line we have invoked ’ReadLine()’ method of the ’Console’ class. This will result in displaying a cursor on the output window waiting for entering any key by the user. When user press ’Enter’ key, the application will be closed. If we don’t write this line of code, then the console output window will be closed immediately when you run the application and you could not be able to see the output.

To test the application either click menu item ’Debug > Run’ or press key ’F5’. This will compile and run the application and you will see the Console window showing the string ’Hello World!”. After inspecting the output enter the ’Enter’ key to close the application.

In this topic we have created our first c# console application. Then we have tested it by compiling and running it. We have also discussed the basic C# program structure.

C# Fundamentals