Compile and Run C# program without using Visual Studio

To get a better understanding how the C# code is compiled let’s create a basic program in C# without using Visual Studio (or any other IDE) . We will use the command-line to compile and execute our C# program.

1. Create File

Create a text file and name it – ‘test.cs’. Make sure the extension of your file is .cs not .txt

2. Add Class

Open your test.cs and create a class named Test

3. Open Developer command prompt

From the Start menu expand ‘Visual Studio’ and open developer command prompt.

4. Compile program

To compile program ‘csc’ command is used. Locate your file and run the command

But we get error that no entry point found. We need to add static ‘Main’ method which serves as the entry point to the C# program.

5. Add Main method

Let’s add main method and add a line print ‘Hello World’ on the console.

compile again and see if get our ‘Hello World’ printed on console :

Oh!! we got another error. Look like we missed something. Yes, we forgot to add System namespace which has Console implementation.

6. Add System namespace

Add System namespace on top of your program. So the whole code now looks like this:

Now compile again, you will see no errors but nothing is printed on console as well.

So, what happened on ‘csc’ command, it created an ‘exe‘ for you having same name as your .cs file. So, you can check for ‘test.exe’ created in the same location as your test.cs

7. Run test.exe

Voila! your first command line program executed successfully.