Language Features for Targeting the CLR
This topic presents an overview of the syntax for writing applications to target the common language runtime.
The syntax is not part of the ISO/ANSI C++ standard but is a set of extensions to C++ standardized under the Ecma C++/CLI Standard.
This syntax replaces Managed Extensions for C++. Managed Extensions for C++ syntax is still available as a deprecated feature under the /clr:oldSyntax compiler option. To port code using Managed Extensions for C++, see Converting Managed Extensions for C++ Syntax.
This syntax is in effect by default when you use /clr.
For information on other features in Visual C++, see
CLR Data Type Keywords
The following table lists keywords that have been added to Visual C++. Note that some keywords consist of two words separated by white space. These aggregate keywords are considered keywords despite the fact that, used separately, they have different meanings. The word ref, for example, used without class is not a keyword and can be used as a regular identifier. Likewise, by itself class denotes a native class. But, used together, ref class defines a common language runtime (CLR) reference type.
Keyword |
Context sensitive |
Purpose |
Reference |
---|---|---|---|
|
No |
Defines a CLR reference class |
|
|
No |
Defines a CLR value class |
|
|
No |
Defines a CLR interface |
|
|
No |
Defines a CLR enumeration |
|
|
Yes |
Defines a CLR property |
|
|
Yes |
Defines a CLR delegate |
|
|
Yes |
Defines a CLR event |
Override Specifiers
The following keywords can be used to qualify override behavior for derivation. The new keyword is not new to C++ but is listed here because of the additional context in which it can be used. Some specifiers are also valid for native programming. For more information, see How to: Declare Override Specifiers in Native Compilations.
Keyword |
Context Sensitive |
Purpose |
Reference |
---|---|---|---|
|
Yes |
Indicates functions or classes are abstract |
|
|
No |
Indicates that a function is not an override of a base class version |
|
|
Yes |
Indicates that a method must be an override of a base-class version |
|
|
Yes |
Prevents classes from being used as base classes |
Keywords for Generics
The following keywords have been added to support generic types. For more information, see Generics (Visual C++).
Keyword |
Context sensitive |
Purpose |
---|---|---|
|
No |
Defines a generic type |
|
Yes |
Specifies the constraints of a generic typef |
Miscellaneous New Keywords
The following keywords have been added to Visual C++.
Keyword |
Context sensitive |
Purpose |
Reference |
---|---|---|---|
|
Yes |
Indicates default exception handlings behavior |
|
|
No |
Enumerate elements of a collection. |
|
|
No |
Allocates types on the garbage-collected heap. Use instead of new and delete. |
|
|
Yes |
Indicates a member can only be initialized at declaration or in a static constructor |
|
|
Yes |
Creates a literal variable |
|
|
No |
Indicates that a handle or pointer does not point at an object |
Non-Keyword Language Constructs
The following are language constructs that, while not official keywords, are fundamental to .NET programming. These constructs are implemented as templates. Most are defined in the cli Namespace.
Keyword |
Purpose |
Reference |
---|---|---|
|
Type for representing CLR arrays |
|
|
Points to data inside reference types. |
|
|
Points to CLR reference types to temporarily suppress the garbage collection system |
|
|
Determines and executes the optimal casting method for CLR types |
|
|
Retrieves a System.Type object describing the given type or object |
Operators
Two operators have been added to Visual C++ to support garbage-collected programming.
Operator |
Purpose |
Reference |
---|---|---|
|
Indicates a handle to an object located on the garbage-collected heap |
|
|
Indicates a tracking reference |
More CLR Constructs
In addition to keywords and language constructs, this reference section includes the following CLR programming constructs.
Topic |
Description |
---|---|
Enables the use of keywords as identifiers |
|
Enables a function to take a variable number of arguments. |
|
Lists the CLR types used in place of C++ integral types |
|
appdomain __declspec modifier |
__declspec modifier that mandates static and global variables exist per appdomain |
Describes how C-style casts are interpreted |
|
__clrcall calling convention |
Indicates the CLR-compliant calling convention |
__cplusplus_cli |
|
Describes how to define your own CLR attributes. |
|
Provides an overview of exception handling |
|
Demonstrates how member functions can override arbitrary members |
|
Discusses how a client assembly can access all types in an assembly component. |
|
Demonstrates the conditions where values types are boxed |
|
Discusses how to detect characteristics of types at compile time. |
|
managed, unmanaged pragmas |
Demonstrates how managed and unmanaged functions can co-exist in the same module |
process __declspec modifier |
__declspec modifier that mandates static and global variables exist per process |
Demonstrates the CLR version of run-time type information |
|
Discusses compiler conversion of string literals to String. |
|
Allows you to move a type in a shipping assembly to another assembly, such that, client code does not have to be recompiled. |
|
Demonstrates use-defined attributes |
|
Imports external assemblies |
|
Explains XML-based code documentation using /doc (Process Documentation Comments) (C/C++) |