C Sharp

Write your first program and how to compile from command line

Actually when you install Visual Studio, then visual studio install some tools and in these tools there some command line tools that you can use for compiling of different programs written in different programming languages.

The seen behind the visual studio is that when you write a program in the programming language visual studio compile that program using the respective compiler for that language.

For example, if you write a program in C# then the visual studio compile that specific program using c sharp compiler that is CSC.

You can find that compiler in the following directory

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

C:\Windows\Microsoft.NET\Framework\ here you can see the different version of compiler available such as \v2.0.50727 or \v3.5 etc.

There are some different feature of various versions so you should use the latest one available. In my case here is v4.0.30319\csc.exe

So if you want to know the version of compiler just run the C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe in your command prompt

In the above pic, you can see the compiler version and the C# version. So in this way you can check out your compiler version.

Now we start to write our first program, and we will compile that program using c sharp compiler without using the visual studio.

Step No. 1

Open notepad and the write below program

class Program
{
    public static Main()
    {
        System.Console.WriteLine("Hello World!");
    }
}

 

OR

using System
class Program
{
   public static Main()
   {
      System.Console.WriteLine("Hello World!");
   }
}

In the above System is the namespace where Console is written.

Step No. 2

File > Save As > first.cs

In above first is the name of file and .cs is the extension of c sharp file. If you don’t save as .cs, then it will not be compiled.

Step No. 3

Now go to directory where you have saved your program open command prompt there

To open the command prompt in that window just hold down shift key and right click (shift + right click)

Step No. 4 Compile

When you opened command prompt then give the path of your compiler just type the below command in your command prompt

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc first.cs

You got the first.exe file that is the compiled version of first.cs. first.exe is an executable file you can run it on the computer without a compiler.

fundamental of c sharp output

Just type the first.exe and hit enter the output of the program will be print finally.

 

But you can compile your program without out giving the compiler path if your compiler path is added to the system environment variable. If you don’t’ know the environment path read this