2 Overview of Managed Types
Managed classes are an annotated subset of C++ classes that are compiled directly into the object model of the common language runtime. They provide full access to the runtime features:
- Extensible metadata. This is information that fully describes the types and interfaces provided by a managed component. This is used in producing software components. The inconvenience of COM type libraries and limitations of OLE automation types are removed with managed classes.
- Garbage collection. The Managed Extensions allow the programmer to specify that certain classes be allocated exclusively on the garbage-collected heap. This removes the burden of lifetime management for the programmer. AddRef/Release bugs and circular-reference leaks cannot occur with managed classes.
- Simplified language interoperability. Any managed class is immediately usable from any language that targets the object model of the common language runtime. MIDL files and BSTRs are no longer required. Other Microsoft Visual Studio .NET languages that target the common language runtime include Visual Basic, Visual C#, and JScript. Third parties are currently developing translators for APL, COBOL, Eiffel, Haskell, ML, Oberon, Objective CAML, Pascal, Perl, Python, Scheme, and Smalltalk.
- Versioning. New methods and data members can be added to managed classes without breaking binary compatibility with existing client software. This eliminates a common problem with C++ class libraries.
- Binary headers. Any file containing metadata (.exe, .obj, .dll, or .netmodule) can be referenced from a C++ source file. This has several advantages:
- The precompiled form speeds compilation over text files.
- Unlike header files. metadata cannot get "out of date" with its associated executable code.
- Files containing metadata are more manageable than precompiled header (PCH) files since they are more granular.
Managed types consist of classes, enums, pointers, and references. There are three kinds of managed classes:
- A __gc class (Section 4) is allocated on the common language runtime heap and is garbage collected. It is the most general-purpose managed class.
- A __value class (Section 5) is designed to represent small, short-lived data items for which full garbage collection would be too costly.
- A __gc interface (Section 6) offers direct support for COM-style interface programming in C++.
An ordinary C++ class is called an unmanaged class.
Each of these is described in detail in this specification.