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.
- Download .NET Framework and install the dev packs.
- 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();
}
}
}
Steps to compile and run the program:
- Open cmd
- Navigate to path and then run
csc A.cs
- 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();
}
}
}
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");
}
}
}
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:
- Create a dll for B using
csc /target:library B.cs
(It generates B.dll file) - Now compile A using
csc /r:B.dll A.cs
(It generates A.exe file) - Run exe using
A
Top comments (5)
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
anddotnet 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:
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. 🎉
Hi,
How can we do that on WSL with .NET 7?
I tryed the following but without luck
interesting one let me try.
Your article title is a bit confusing. You should substitute 'Execute' with 'Compile'.
Thank you, updated.