Last Updated:

Relationship between C# (Csharp) and .NET

C# is a relatively new programming language characterized by the following advantages:

  • It is designed specifically for use with the Microsoft .NET Framework (an advanced platform for developing, deploying, and running distributed applications).
  • This is a language based on a modern object-oriented design methodology, in the development of which Microsoft specialists relied on the experience of creating similar languages built in accordance with object-oriented principles that were first proposed about 20 years ago.

C# is an independent programming language. Although it is designed to generate code that runs in a .NET environment, it is not itself part of .NET. However, because C# is designed to be used on the .NET platform, it is important for you as a developer to have an understanding of the .NET Framework if you want to develop C# applications efficiently.

Common Language Runtime (CLR)

The central part of the .NET framework is its Common Language Runtime (CLR), or .NET runtime.

Code that runs under clR control is often referred to as managed code.

However, before code can be executed by the CLR, any source code (in C# or another language) must be compiled. Compiling in .NET consists of two steps:

  1. Compiling the source code into IL.
  2. Compile IL into platform-specific code using the CLR.

This two-step compilation process is very important because having IL (managed code) is key to many of the benefits of .NET.

Benefits of Managed Code

Microsoft Intermediate Language (MSIL) shares with Java bytecode the idea of a low-level language with a simple syntax (based on numeric codes instead of text) that can be translated very quickly into native machine code. Having this code with a well-defined universal syntax provides a number of significant advantages.

This means that a file containing bakt code instructions can be placed on any platform; at runtime, the final stage of compilation can be easily accomplished, allowing the code to be executed on a specific platform. In other words, by compiling to IL, you gain .NET platform independence.

Performance Enhancement

  IL is always compiled quickly (Just-In-Time, also known as JIT compilation). Instead of compiling the entire application in a single pass (which can cause delays at startup), the JIT compiler simply compiles each piece of code when it is called (just-in-time - promptly). If the intermediate code is compiled once, the resulting native executable code persists until the application shuts down, so it does not need to be recompiled during repeated calls.

Microsofr argues that such a process is more efficient than copying the entire application at startup, since there is a high probability that large chunks of application code will not actually be executed every time it is launched. If you use a just-in-time (JIT) compiler, such code will never be compiled.

This explains why you can expect that the execution of native managed IL code will be almost as fast as the execution of native native code.

The final stage of compilation takes place at runtime, the JIT compiler at this point already knows on what type of processor the program will run.

This means that it can optimize the final executable ko by using instructions of specific machine code intended for a specific processor.

Traditional compilers optimize code, but they can only perform optimizations that are independent of the specific processor on which the code will be executed. This is because traditional compilers generate executable programs before they are delivered to users.

Linguistic ability to interact

The use of IL not only provides platform independence; it also provides interoperability. Just imagine that you can compile a program in one language into IL code, and this compiled code will be able to interact freely with IL code compiled from another language.