Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
vermeir_nico_introducing_net_6_getting_started_with_blazor_m.pdf
Скачиваний:
19
Добавлен:
26.06.2023
Размер:
11.64 Mб
Скачать

Chapter 2 Runtimes and Desktop Packs

Figure 2-1.  .NET unification

What the image portraits is the .NET abstraction layer. We write the same .NET code everywhere, but depending on the compile target, a different compiler will be used. When executing a .NET application, a different runtime may be used depending on the platform it is being executed on. Let’s take a command line application, for example, a command line has no UI so no platform-specific code to render screens is necessary, meaning that the same CLI application can run on Windows, Linux, and macOS. When compiling this application, the default .NET 6 compiler will be used, resulting in one executable. Running this executable on Windows will be handled by the common language runtime, CoreCLR. On macOS and Linux, however, this will be handled by Mono, completely transparent to developers and users.

Runtimes

The .NET languages are managed languages, meaning that code you write in C# gets compiled down to intermediate language. Once your code gets executed, that

intermediate language is compiled into machine code by the just in time compiler, or JIT. That JIT is part of the common language runtime, or CLR.

22

Chapter 2 Runtimes and Desktop Packs

When writing .NET code, we don’t program against an operating system; the system APIs in C# don’t target Windows/Linux/macOS directly; instead, they target the API surface of the common language runtime called CoreFX. CoreFX is the newer name of what used to be the Base Class Library or BCL. It includes the System.* namespaces that we use all the time to call platform or framework APIs. The CLR calls into the operating system’s APIs via CoreFX to perform the tasks requested by the developer. In this way, the CLR functions as an abstraction layer, enabling cross-platform code.

The CLR also gives us memory management, keeping track of objects in memory and releasing them when they are no longer needed. This garbage collection is part of the runtime and is what makes .NET languages managed, compared to unmanaged languages like C and C++ where you must do your own memory management.

.NET 6 contains two default runtimes. Depending on the platform you are running your code on, it will be executed by either CoreCLR or Mono.

The .NET 6 runtimes are open source and available at https://github.com/dotnet/runtime.

CoreCLR

The CoreCLR is the .NET 6 version of the classic CLR. It is the common language runtime used for running .NET code on Windows. No matter if it is a desktop application, web application, or console app, if any of these run on Windows, they will use the CoreCLR.

Mono

Mono started as an open-source project to bring .NET and its languages to Linux. Mono was based on the publication of the .NET open standard. The first version of Mono

was released in 2004. The maintainers of the Mono open-source project were a small company called Ximian. Ximian and thus Mono were acquired by Novell, Novell was acquired by Attachmate, and the future of Mono seemed very dark. Some people from Ximian formed a new company called Xamarin. Xamarin continued the work on Mono, eventually releasing a mobile cross-platform framework based on Mono. Microsoft became the owner of the Mono project after acquiring Xamarin in 2016.

Mono currently ships as part of .NET 6; it is the default runtime when not running on a Windows-based operating system.

23