The Common Language Runtime

One of the major components of the .NET Framework is the Common Language Runtime, or CLR. The CLR provides a number of benefits to the developer, such as exception handling, security, debugging, and versioning, and these benefits are available to any language built for the CLR. This means that the CLR can host a variety of languages, and can offer a common set of tools across those languages. Microsoft has made VB .NET, C++.NET, and C# “premier” languages for the CLR, which means that these three languages fully support the CLR. In addition, other vendors have signed up to provide implementations of other languages, such as Perl, Python, and even COBOL.

When a compiler compiles for the CLR, this code is said to be managed code. Managed code is simply code that takes advantage of the services offered by the CLR. For the runtime to work with managed code, that code must contain metadata. This metadata is created during the compilation process by compilers targeting the CLR. The metadata is stored with the compiled code and contains information about the types, members, and references in the code. Among other things, the CLR uses this metadata to

  • Locate classes

  • Load classes

  • Generate native code

  • Provide security

If you think about it, the tasks above used to be handled by COM and the registry. One of .NET's goals is to allow applications to be distributed without the need to use the registry. In fact, .NET components can be copied into a directory and used, without the need for a registration process. .NET handles locating and loading the objects from the component, which replaces work that used to be handled by COM.

The runtime also handles object lifetimes. Just as COM/COM+ provided reference counting for objects, the CLR manages references to objects and removes them from memory when all the references are gone, through the process known as garbage collection. Although garbage collection actually gives you slightly less control than you had in VB, you gain some important benefits. For example, your errors should decrease because the number of objects that hang around due to circular references should be reduced or completely eliminated because .NET has logic for handling objects that are only involved in circular references. In addition, garbage collection ends up being much faster than the old way of destroying objects in VB. Instances of objects you create that are managed by the runtime are called managed data. You can interact with both managed and unmanaged data in the same application, although managed data gives you all the benefits of the runtime.

The CLR also defines a standard type system to be used by all CLR languages. This means that all CLR languages will have the same size integers and longs, and they will all have the same type of string—no more worrying about BStrs and CStrs! This standard type system opens up the door for some powerful language interoperability. For example, you can pass a reference of a class from one component to another, even if those components are written in different languages. You also can derive a class in C# from a base class written in VB .NET, or any other combination of languages targeted to the runtime. Don't forget that COM had a set of standard types as well, but they were binary standards. This meant that with COM, you had language interoperability at run time. With .NET's type standard, you have language interoperability at design time.

After it is compiled, managed code includes metadata, which contains information about the component itself, and the components used to create the code. The runtime can check to make sure that resources on which you depend are available. The metadata removes the need to store component information in the registry. That means moving a component to a new machine does not require registration (unless it will be a global assembly, which is described in Chapter 4, “Building Classes and Assemblies with VB .NET”), and removing components is as simple as deleting them.

As you can see, the Common Language Runtime provides a number of benefits that are not only new, but should enhance the experience of building applications. Other benefits that you will see in more detail include some of the new object-oriented features to VB .NET. Many of these new features are not so much additions to the language as they are features of the runtime that are simply being exposed to the VB .NET.

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

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