Common Language Runtime (CLR)

CLR is a runtime environment that sits in the memory of the underlying machine and converts the IL code to native code. The native code is specific to the underlying platform in which the code is running. This provides a platform independence feature in a typical application made on .NET Framework. Some of the other features provided by CLR are mentioned here:

  • Memory management: CLR provides automatic allocation and release of memory across the application. Due to this, developers do not need to explicitly write code to manage memory. This eliminates issues that can lead to degradation of application performance due to memory leaks. CLR manages the allocation and removal of memory using a garbage collector, which manages the memory allocation in the following manner:
    • Allocating memory: When an application is executed in CLR, it reserves a continuous space of memory for its execution. The reserved space is known as a managed heap. The heap maintains a pointer to the memory address where the next object defined in the process will be allocated. 
    • Releasing memory: During the runtime execution of the program, the garbage collector runs at scheduled times and examines whether the memory allocated in heaps are still in scope of program execution or not. 
    • It determines whether the program is still using the memory on the basis of roots or the collection of memory objects are still in the scope of the program. If any memory allocation is not reachable as per the collection in the root, the garbage collector determines that the memory allocated in that memory space can be released.
    • We will look into memory management in detail in Chapter 9, Manage the Object Life Cycle.
  • Exception handling: When an application is being executed, it may result in certain execution paths that could generate some errors in the application. Some of the common examples are as follows:
    • When an application tries to access an object such as a file that is not present in the specified directory path.
    • When an application tries to execute a query on the database but the connection between the application and the underlying database is broken/not open.
    • We will look into exception handling in detail when we go through Chapter 7, Implementing Exception Handling.

In the next section, we will look at the release history of .NET Framework and its compatibility with different versions of CLR and C#.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.14.98