What you need to know about the .NET Framework—
Jeffrey Richter

Jeffrey Richter specializes in programming/designing for .NET and Win32 and is an expert on Metadata. As a preview of his VCDC session "Metadata and Building Applications" read his VSLive! speaker interview below!




Can you give a brief overview of the .NET Framework?
The .NET Framework consist of the common language runtime (CLR) and the Framework Class Library (FCL). The CLR defines how types are defined and how they behave. Compilers allow developers to use the types that have already been defined by Microsoft's FCL. There are over 7,000 types in the FCL today and many more will be added in the future. Of course, the FCL defines the common types that all programmers use: integers, strings, Booleans, characters, and so on. However, a programming language can allow a developer to define new types. Any new type is instantly usable by the CLR and by any other programming language.

I know a lot of developers that want to learn C#, Visual Basic .NET, or some other language; however, a programming language should not be the focus of a developer's attention. Programmers should learn how the CLR works and should learn the types contained in the FCL. This is where the learning curve is. Programming languages are just about the syntax required for a programmer to express their intentions. A programming language is about semicolons and braces. This kind of stuff is easy to learn and pick up. The CLR and the FCL is the heat of the .NET Framework and a solid understanding of these 2 technologies will help developers go far regardless of the programming language they use.

What are some of the more important features offered by the .NET Framework's CLR?
The CLR offers several features that it makes available to all programming languages. First, the CLR ensures that code is type safe and that it only performs "safe" operation. This means that a developer's code can't perform any operation that might corrupt memory and ensures that an object's state never becomes corrupt. If the programmer's code attempts to do something "improper" the CLR catches the problem and throws an exception alerting the developer to the problem. This helps developers write more bug-free code.

Second, the CLR also offers Code Access Security which allows for fine-granularity of protection. For example it's possible to write access to a specific disk subdirectory while allowing unrestricted read access.

Third, the CLR proves rich threading capabilities to all applications written in any programming language allowing developers to write high-performance, scalable applications.

Fourth, the CLR imposes a garbage collection environment on all languages. Via garbage collection, an application cannot corrupt memory and the responsibility for knowing when to free an object is the job of the garbage collection. This frees developers from having to worry about memory management and allows them to focus on their applications logic.

Fifth, the CLR uses exception handing throughout. Exception handling allows developers to separate their method's cleanup and error recovery code from the method's main logic. This makes it easier to write, read, and maintain code. In addition, the developer cannot simply ignore an error returned by a method allowing the application to continue to run unpredictably. The developer must catch the exception and recover from the problem. These are just some of the highlights offered by the CLR; there are many more features available.

What is the role of Metadata in the .NET Framework?
In my opinion, metadata is the cornerstone and most important part of the .NET Framework. For those that don't know, metadata is a set of tables all compilers must produce when compiling source code. There are two main kinds of metadata tables: definition tables and reference tables. The definition tables describe the types and members that were defined in source code. This information is interpreted by the CLR and it is this metadata that tells the CLR about the developer's types allowing them to be part of the "system" so that the types may be used by any programming language. In a way, metadata is the common denominator for all programming languages.

Metadata is used to enable lots of technologies. For example, the FCL's serialization engines use metadata to know how to serialize a type's fields into a binary or XML stream. Visual Studio's IntelliSense uses metadata to offer the developer suggestions when editing their code. The garbage collector uses metadata to know when fields within an object refer to other objects so that the GC can know what objects can and can't have their memory reclaimed. Metadata also allows compilers to reference types defined in another programming language without any auxiliary information such as header files. And certainly, ASP.NET XML Web services use metadata to determine how to how to expose a type's method as a web service method with little effort by the programmer. ASP.NET also uses the metadata to automatically construct the Web Service Description Language (WSDL) for a web service's methods.

What are custom attributes and what role do they play?
When a developer defines a method, they can define the method using a variety of attributes such as public, static, virtual, and so on. But what if the developer wants to define a method that is not just public but is "public over the Internet"? Compiler's offer no attribute with this semantic. However, the .NET Framework's custom attributes allow anyone (not just Microsoft) to define custom attributes which can be applied to methods (and other targets) in source code.

Let's look at an example. Microsoft's ASP.NET team has created a WebMethod attribute. When this method is applied to a method, ASP.NET knows to make the method "publicly accessible via the Internet". Custom attributes are a pretty innovating thing. Microsoft's FCL defines several custom attributes for various purposes such as security, interoperating with unmanaged code, working with XML, and so on. All .NET Framework developers should become very familiar with custom attributes since Microsoft uses them quite frequently. But, don't' forget, any developer can define their own custom attributes and use them as they see fit — this is a very powerful technology indeed.

How concerned should developers be with the .NET Framework's Performance?
Today, if you were to build an application in unmanaged C++ and in a language like C#, you will probably find that the unmanaged C++ application performs better. But, it really depends on the application you're building. Certainly, the CLR offers a ton of features that you don't get with unmanaged C++ like runtime type safety, code access security and garbage collection. There is some performance cost associated with these technologies. In the long run, I think that .NET Framework applications will actually perform better than unmanaged applications. Let me give you some reason why. First, when you compile an unmanaged C++ application today, you typically tell the compiler to use only 386 CPU instructions. This way, the application will run on the widest variety of CPUs. However, the CLR compiles code into CPU instructions at runtime. This means that if the CLR is running on a Pentium 4 machine, it will use Pentium 4 CPU instructions.

Also, if the CLR sees that the machine is a single processor machine, it can remove any code that is only necessary for multi-processor machines. Likewise, if the CLR sees that a certain 'if' statement will never be true on the host machine, the CLR can remove the 'if' test and its code entirely when compiling to CPU instructions.

Also, the CLR ensures that a type's fields are aligned correctly on proper byte boundaries which can improve performance enormously on some CPU architecture. Theoretically, the CLR could analyze a method's code path at execution time and could re-arrange or recompile the code as the application runs fine tuning the code for the way the application is actually running. For example, if the CLR sees that a test is always false, it could rearrange the code to improve branch predictions.


Jeffrey Richter
Jeffrey Richter (
www.JeffreyRichter.com) is the author of Programming Applications for Microsoft Windows (Microsoft Press, 1999), and is a co-founder of Wintellect, a software education and consulting firm. He specializes in programming/design for .NET and Win32. Jeff is currently authoring the book Programming .NET Framework Applications and offers .NET technology seminars