How Does C# Code Get Compiled and Executed?

Onur Derman
Level Up Coding
Published in
4 min readMar 13, 2023

--

When you write C# code and execute the program within the .NET framework, how does the computer interpret the plain text code and produce the desired output?? In this article we are going to learn about this whole process

C# is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. .NET is a software development framework that provides a set of tools and libraries for building applications that run on Windows, macOS, and Linux. C# is designed to be simple, powerful, and type-safe, and is widely used for building desktop applications, web applications, and games.

When writing C# code, it is compiled into Microsoft Intermediate Language (MSIL) or Intermediate Language (IL) code, which is an assembly-level language that is platform-independent. The compiled code is then packaged into a binary format that can be executed on the target platform. This binary format can take several forms, including Dynamic Link Libraries (DLLs) and Executable Files (EXEs).

DLLs are shared libraries that contain code and data that can be used by multiple applications. When a DLL is loaded into memory, it can be used by any application that references it, allowing multiple applications to share the same code and resources. DLLs can also be updated independently of the applications that use them, making it easier to deploy updates and fixes.

EXEs are standalone applications that can be executed on the target platform. They contain all the code and resources needed to run the application, including any DLLs that the application depends on. When an EXE is executed, it is loaded into memory and executed by the operating system.

Both DLLs and EXEs can be compiled from C# code using the C# compiler. The compiled code is packaged into assemblies, which are binary files that contain the compiled code and any metadata that describes the code. Assemblies can be stored in DLLs or EXEs, depending on how they will be used.

The Common Language Runtime (CLR) is responsible for managing the execution of both DLLs and EXEs. When an assembly is loaded into the CLR, the CLR performs a number of checks to ensure that the assembly is safe to execute. This includes verifying that the assembly has not been tampered with and that it is signed with a valid digital signature.

The Just-In-Time (JIT) compiler is responsible for compiling the MSIL code into native machine code that can be executed on the computer’s processor. This compilation process happens dynamically at runtime, and the resulting native machine code is optimized for the specific hardware and operating system on which it is running.

Now, let’s take a look at the process of compiling and executing C# code step by step:

  1. Writing C# Code: The first step in building a C# application is to write the code. This code can be written in any text editor or integrated development environment (IDE), such as Visual Studio, Visual Studio Code…
  2. Compiling C# Code: Once the code is written, it needs to be compiled into MSIL code using the C# compiler. This creates an assembly that contains the compiled code and any metadata that describes the code.
  3. Loading the Assembly: The assembly is loaded into the CLR, which loads the code into memory and performs a number of checks to ensure that the code is safe to execute. This includes verifying that the code has not been tampered with and that it is signed with a valid digital signature.
  4. JIT Compilation: The MSIL code is then compiled by the Just-In-Time (JIT) compiler into native machine code that can be executed on the computer’s processor. This code is generated dynamically at runtime and is optimized for the specific hardware and operating system on which it is running.
  5. Execution: The native machine code can now be executed by the computer’s processor, which runs the C# application. During execution, the CLR manages the memory and resources used by the application, including performing garbage collection to free up memory that is no longer being used.

I appreciate your participation and hope that my article has provided you with valuable insights. Thank you for taking the time to read it thus far.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--