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 (2)
Your article title is a bit confusing. You should substitute 'Execute' with 'Compile'.
Thank you, updated.