Compile and Run C# programs without visual studio or any other IDE

Bharath Muppa - Dec 1 '21 - - Dev Community

In this Article, We are going to explore how to Compile and Run your C# programs without using any IDE.

Even Before thinking of doing it, lets ask ourselves this simple question.

Q: Why? Am I going back to stone age?
Ans: No, Writing sample codes in any language without IDE and compiling it will help you understand the eco system better.

Lets move on to Implementation part.

Download and install.

  1. Download .NET Framework and install the dev packs.
  2. Then add C:\Windows\Microsoft.NET\Framework64\v4.0.30319 (it can be any version you download) to environment path.

Step 2 will make csc (c sharp compiler) executable to be available to access.

Coding Part

1. Compile and Run Class A

Open Notepad and copy class below and name it as A.cs


// C# program to print Hello World!
using System;


// namespace declaration
namespace HelloWorldApp {

// Class declaration
class A {

    // Main Method
    static void Main(string[] args)
    {

        // printing Hello World!
        Console.WriteLine("Hello World!");
        // To prevents the screen from
        // running and closing quickly
        Console.ReadKey();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Steps to compile and run the program:

  1. Open cmd
  2. Navigate to path and then run csc A.cs
  3. Then run command A.

2. Compile and Run Class A, which uses Class B

Open notepad and copy below code and name it as A.cs


using System;

// namespace declaration
namespace HelloWorldApp {

// Class declaration
class A{
    // Main Method
    static void Main(string[] args)
    {
            // printing Hello World!
        Console.WriteLine("Hello World!");
                var b = new B();
        b.startMusic();
        // To prevents the screen from
        // running and closing quickly
        Console.ReadKey();
    }
   }
}

Enter fullscreen mode Exit fullscreen mode

open notepad and copy below code and name it as B.cs

using System;

// namespace declaration
namespace HelloWorldApp
{
    public class B{
            public void startMusic(){
               Console.Write("I am in B");
            }   
    }
}

Enter fullscreen mode Exit fullscreen mode

If you run the above steps as mentioned before to compile and run, you will see compile time error saying B not found.

it is obvious, because we didn't create any assembly or sln in visual studio or other IDE that usually does most of the heavy lifting.

So, we need to create dll for B and refer that dll while compiling A.(So that A knows there exists a library which goes by name B)

New Steps to compile and run:

  1. Create a dll for B using csc /target:library B.cs(It generates B.dll file)
  2. Now compile A using csc /r:B.dll A.cs(It generates A.exe file)
  3. Run exe using A
. . . . . . . . . . . . . . .