Compilation and Execution of a C# Program

To compile and execute a program in C#, you can use several methods depending on your development environment. The most common approaches include using Visual Studio IDE, command-line tools, or online compilers.

Using Visual Studio IDE

In Microsoft Visual Studio IDE, compilation and execution is straightforward −

  • Click the Run button or press F5 key to build and execute the project.

  • Visual Studio automatically compiles your code and runs the executable.

  • Any compilation errors will appear in the Error List window.

Command-Line Compilation

You can compile C# programs using the command-line C# compiler (csc.exe) without Visual Studio −

Step-by-Step Process

  1. Open a text editor and create your C# program.

  2. Save the file with a .cs extension (e.g., helloworld.cs).

  3. Open the command prompt and navigate to the directory containing your file.

  4. Type csc helloworld.cs and press Enter to compile.

  5. If compilation succeeds, an executable file helloworld.exe is generated.

  6. Type helloworld to execute your program.

Example

Create a simple C# program to demonstrate the compilation process −

using System;

class HelloWorld {
   public static void Main() {
      Console.WriteLine("Hello World!");
   }
}

Save this as HelloWorld.cs, then compile and run −

C:\> csc HelloWorld.cs
C:\> HelloWorld

The output will be −

Hello World!

Using .NET CLI

For modern .NET development, you can use the .NET CLI tools −

dotnet new console -n MyApp
cd MyApp
dotnet run

This creates a new console application and runs it immediately.

C# Compilation Process

C# Compilation and Execution Process Source Code .cs files C# Compiler csc.exe or dotnet build IL Code .exe/.dll Bytecode CLR/JIT Runtime Execution Machine Code Generation JIT compiles IL to native code

Common Compilation Options

Command Description
csc filename.cs Compile a single C# file
csc /out:myapp.exe *.cs Compile multiple files with custom output name
csc /target:library mylib.cs Compile to a library (.dll)
dotnet run Build and run using .NET CLI

Online Compilers

For quick testing and learning, you can use C# online compiler to write, compile, and execute C# programs directly in your web browser without installing any development tools.

Conclusion

C# programs can be compiled using Visual Studio IDE, command-line tools like csc.exe, or modern .NET CLI commands. The compilation process converts C# source code into Intermediate Language (IL) bytecode, which is then executed by the Common Language Runtime (CLR).

Updated on: 2026-03-17T07:04:35+05:30

22K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements