Application Domains Common Language Specification CLS

116 As it happens, this module is part of a single-module assembly. Because assemblies are named after the module that contains the assembly manifest, the assembly is called System.Drawing. A compiler will reference this assembly as System.Drawing.dll. Many but not all of the types in this assembly have a namespace of System.Drawing. Other types in the System.Drawing assembly have namespaces of System.Drawing.Design, System.Drawing.Drawing2D, System.Drawing.Imaging, System.Drawing.Printing, and System.Drawing.Text. Note that even though the namespace, module, and assembly are similarly named in this case, they are distinct concepts. Note in particular that importing a namespace and referencing an assembly are different operations with different purposes. The statement: Imports System.Drawing allows the developer to avoid typing the fully qualified names of the types in the System.Drawing namespace. It does not reference the assembly in which those types are defined. To use the types, the System.Drawing assembly contained in the System.Drawing.dll module must be referenced as described earlier in this section. The Imports statement was introduced in Chapt er 2 . In other cases, namespace and assembly names dont correspond. One example is the System namespace. Some types with this namespace are found in the mscorlib assembly, and others are found in the System assembly. In addition, each of those assemblies has types with other namespaces. For example, the System assembly contains types with the Microsoft.VisualBasic namespace, even though most of the types with that namespace are found in the Microsoft.VisualBasic assembly. The reason for this apparent inconsistency is actually quite sound. Namespaces group types according to functionality, while assemblies tend to group types according to which types are most likely to be used together. This improves performance because it minimizes the number of assemblies that have to be loaded at runtime. When thinking about namespaces, just remember that types can have any namespace at all, regardless of where theyre defined—the namespace is just part of the type name.

3.5 Application Domains

Application domains are to the CLR what processes are to an operating system. It may be surprising to note that the CLR can run multiple .NET applications within a single process, without any contention or security difficulties. Because the CLR has complete control over loading and executing programs, and because of the presence of type information, the CLR guarantees that .NET applications cannot read or write each others memory, even when running in the same process. Because there is less performance overhead in switching between application domains than in switching between processes, this provides a performance gain. This is especially beneficial to web applications running in Internet Information Services IIS, where scalability is an issue.

3.6 Common Language Specification CLS

The CLI defines a runtime that is capable of supporting most, if not all, of the features found in modern programming languages. It is not intended that all languages that target the CLR will support all CLR features. This could cause problems when components written in different languages attempt to interoperate. The CLI therefore defines a subset of features that are considered compatible across language boundaries. This subset is called the Common Language Specification CLS. Vendors creating components for use by others need to ensure that all externally visible constructs e.g., public types, public and protected methods, parameters on public and protected methods, etc. are CLS-compliant. This ensures that their components will be usable within a broad array of languages, including Visual Basic .NET. Developers authoring components in Visual Basic .NET have an easy job because all Visual Basic .NET code is CLS-compliant unless the developer explicitly exposes a public or protected type member or method parameter that is of a non-CLS-compliant type. 117 Because Visual Basic .NET automatically generates CLS-compliant components, this book does not describe the CLS rules. However, to give you a sense of the kind of thing that the CLS specifies, consider that some languages support a feature called operator overloading . This allows the developer to specify actions that should be taken if the standard operator symbols + , - , , , = , etc. are used on user-defined classes. Because it is not reasonable to expect that all languages should implement such a feature, the CLS has a rule about it. The rule states that if a CLS-compliant component has public types that provide overloaded operators, those types must provide access to that functionality in another way as well usually by providing a public method that performs the same operation.

3.7 Intermediate Language IL and Just-In-Time JIT Compilation