Appendix C. Introduction to .NET

.NET is based on a Common Language Runtime (CLR) environment that manages every runtime aspect of your code. All .NET components, regardless of the language in which they are developed, execute in the same runtime (hence the name). The CLR is like a warm blanket that surrounds your code, providing it with memory management, a secure environment to run in, object location transparency, concurrency management, and access to the underlying operating system services. Because the CLR manages these aspects of your object’s behavior, code that targets the CLR is called managed code. The CLR provides absolute language interoperability, allowing a high degree of component interoperability. COM, too, provides language independence, allowing binary components developed in two different languages (such as Visual Basic and C++) to call one another’s methods, but COM language interoperability is only at runtime. During development, .NET allows a component developed in one language to derive from a component developed in another language seamlessly. .NET is capable of this process because the CLR is based on a strict type system. To qualify as a .NET language, all constructs (such as class, struct, or primitive types) in every language must compile to CLR-compatible types. The language interoperability gain is at the expense of existing languages and compilers. Existing compilers produce CLR-ignorant code—code that does not comply with the CLR type system and that is not managed by the CLR. Visual Studio.NET comes with four CLR-compliant languages: C#, Visual Basic.NET, JScript.NET, and Managed C++. Third-party compiler vendors also target the CLR, with more than 20 other languages, from COBOL to Eiffel.

.NET Programming Languages

All .NET programming languages use the same set of base classes, development environment, and CLR types and comply with the same CLR design constraints. Compiling code in .NET is a two-phase process. First, the high-level code is compiled into a generic machine-code-like language called intermediate language (IL). At runtime, on the first call into the IL code, the IL is compiled into native code and executes as native code. The native code is used until the program terminates. The IL is the common denominator of all .NET programming languages, and equivalent constructs in two different languages should theoretically produce identical IL. As a result, all .NET programming languages are equal in performance and ease of development.

The difference between the languages is mostly aesthetic, and choosing one over another is a matter of personal preference. For example, to make C++ CLR compliant, Microsoft had to add numerous nonstandard compiler directives and extensions, resulting in less readable code than standard unmanaged C++. Similarly, Visual Basic.NET bears little resemblance to its Visual Basic 6.0 ancestor, requiring you to unlearn things you used to do in Visual Basic 6.0. Only C# has no legacy and is a fresh .NET language. C# is a C++ derivative language, combining the power of C++ with the ease of Visual Basic 6.0, and offering you readable, CLR-compliant C++ like code. In fact, C# looks more like normal C++ than managed C++. This appendix and Chapter 10 therefore use C# in its code samples. Bear in mind, however, that you can do all the code samples in Visual Basic.NET, managed C++, or any other .NET language.

Other features of .NET languages include their treatment of every entity as an object (including primitive types), resulting in a cleaner programming model. .NET provides common error handling based on exceptions. The CLR has a rich predefined set of exception classes that you can use as is, or derive and extend for a specific need. An exception thrown in one language can be caught and handled in another language.

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

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