C# FAQ 5 - How Do I Write My First C# Program

In this FAQ, we will examine the steps required to develop a simple C# console based program. However, as a first step we will discuss the basic requirements required to begin C# programming.

Requirements

In order to write code for a simple C# program, you can either make use of Notepad which comes with Windows or advanced editors such as Visual Studio, Visual Studio Community or Microsoft WebMatrix including few third party tools and editors. Linux also provide support for C# with the help of Mono C# compiler Kit.

If you make use of Visual Studio, you need not have to install .NET Framework separately. However, if you use Notepad then you have to manually install .NET Framework SDK which includes all the required tools to develop C# applications.

The code which you provide during the creation of a C# program is called source code. It consists of several syntaxes as per the format laid down by Microsoft. However, editors like Visual Studio enable you to automate coding tasks with the help of IntelliSense which is not possible in Notepad.

You should install .NET Framework SDK on your system before getting started with C#. This step is not required if you have installed either Visual Studio Community which is available free of cost or full blown Visual Studio.

Editor Identification

As a first step, you need to identify an editor using which you will write source code. In this article, we will discuss the creation of a simple C# program using Notepad.

Source Code

The next step is to provide the required source code as in the following inside the editor.

  1. using System;  
  2. class HelloWorld  
  3. {  
  4.    public static void Main()  
  5.    {  
  6.       Console.WriteLine("Hello World");  
  7.    }  
  8. }  
If the above code is compiled and executed, it will display Hello World on the command prompt. Let us now examine the above code in detail.

Analyzing C# Code

The first step in .NET Framework program particularly C# is to declare System namespace. If you are not aware, namespaces consist of group of classes, types or assemblies. Each class contains several methods which you need to make use of during programming.

You need to provide a name for your class. This should be your file name although it is not compulsory. In the above code, the class name is defined as HelloWorld and hence the file name should be saved under this name followed by extension .cs

The above source code calls Main() method which is required for all C# programs. Finally, you need to call WriteLine() method of the Console class and provide the required parameter which will be displayed as output.

Saving C# Code

Finally, you should save the file with the name - HelloWorld.cs. As I mentioned above you can save under any file name but we use same name for the class and file for easy identification purpose.

Compiling C# Code

In order to compile the above source code, you need to provide the following statement inside DOS prompt.


csc HelloWorld.cs

If you are unable to compile the above source code, refer to C# FAQ 4 - How do I Configure C# Compiler? where we discuss steps required to configure C# compiler.

The compiler displays error messages which you need to rectify before execution of the code. C# is a case sensitive language like C, C++ and hence you need to follow all guidelines formulated by Microsoft for error free programming.

Sometimes, error occurs even if your source code is correct. This scenario is termed as exception. We will discuss more about this topic in an upcoming FAQ.

Viewing the Output

In order to view the output generated by the above source code, provide the following command on the console.
HelloWorld
You will be able to view an output as shown below
 
 
Let us now examine another source code as in the following,
  1. using System;  
  2. class Add  
  3. {  
  4.    public static void Main()  
  5.    {  
  6.       int a = 5;  
  7.       int b = 6;  
  8.       int c = a+b;  
  9.       Console.WriteLine(c);  
  10.    }  
  11. }  
The above code adds two numbers and returns its value as output. As you can see, we have defined two numbers which are of integer type. Technically, integer is also referred to as data type. The WriteLine() method accepts the added value as a parameters and you will view output as shown below,
 
 
We can modify the way in which the output is displayed. For instance, try to replace the WriteLine() method on the above code with the following code
  1. using System;  
  2. class Add  
  3. {  
  4.    public static void Main()  
  5.    {  
  6.       int a = 5;  
  7.       int b = 6;  
  8.       int c = a+b;  
  9.       Console.WriteLine("The output is " +c);  
  10.    }  
  11. }  

You will view an output as in the following,

In the same way, try to replace WriteLine() method as per the following code and view the output.
  1. using System;  
  2. class Add  
  3. {  
  4.    public static void Main()  
  5.    {  
  6.       int a = 5;  
  7.       int b = 6;  
  8.       int c = a+b;  
  9.       Console.WriteLine("The result of " +a+ " + " +b+ " = " +c);  
  10.    }  
  11. }  
The resulting output will be as in the following,
 
 
The above displayed output is as a result of string concatenation where we tie several strings to produce an output which resembles a sentence. You should compile the source code after every edit of the source code to view its revised output. We hope you have mastered the basic steps required to compile and run a C# program.