DEV Community

Cover image for Compile and Run C# programs without visual studio or any other IDE
Bharath Muppa for Entangled Cognition

Posted on • Updated on

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

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

Top comments (5)

Collapse
 
jongeduard profile image
Eduard • Edited

Okay, weird, but this got my fascination, because in the olden days we could directly call the CSC compiler in a simple way, and I was curious.
But in modern .NET this is all quite different (also worth exploring how to call the compiler from C# source code by the way, which lives inside the Microsoft.CodeAnalysis namespaces, but that's a whole different topic).

First of all, it's good to note that it has very little more practical use than just for fun and learning, because a simple dotnet build and dotnet run these days does whole job for us.

Nevertheless, I basically managed to do it on my Linux system.
Important to understand is that when you execute an entry assembly directly (so not some EXE launcher that dotnet normally also builds for us, but the actual assembly itself), you still have to invoke the .NET runtime as well.
So this also applies to the compiler. You still have to invoke it with the dotnet command.

For me the following does work:

dotnet /usr/share/dotnet/sdk/8.0.202/Roslyn/bincore/csc.dll -target:exe -out:Program.dll -lib:'/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.3/' -reference:System.Private.CoreLib.dll -reference:System.Runtime.dll -reference:System.Console.dll -main:Program Program.cs
Enter fullscreen mode Exit fullscreen mode

So what I did here was not just manually invoking the compiler itself, but also passing the library path of the standard .NET libraries which it needs, as well as the names of the required libraries themselves. This does not happen out of the box.

I also specified the target, output name and the entry point class (main).
At least it looks like you can leave the entry point away, which enables you to use top level code. But for demonstration purpose I liked to leave it in here.

However, after compiling, my program still did not run. :(
An error about a missing 'libhostpolicy.so' dependency appeared. Turns out we really need to have a Program.runtimeconfig.json file as well.
I just copied that from an existing bin directory.

Now, calling dotnet Program.dll did eventually showed me the "Hello, World!" output of my program that I was expecting.
Job done. 🎉

Collapse
 
rafaspimenta profile image
Rafael Pimenta

Hi,
How can we do that on WSL with .NET 7?

I tryed the following but without luck

 î‚° sudo /usr/lib/dotnet/sdk/7.0.110/Roslyn/bincore/csc.dll Program.cs

/usr/lib/dotnet/sdk/7.0.110/Roslyn/bincore/csc.dll: command not found
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bharathmuppa profile image
Bharath Muppa

interesting one let me try.

Collapse
 
baronics profile image
baronics

Your article title is a bit confusing. You should substitute 'Execute' with 'Compile'.

Collapse
 
bharathmuppa profile image
Bharath Muppa

Thank you, updated.