Udostępnij za pośrednictwem


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.

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

ref class
ref struct

No

Defines a CLR reference class

Classes and Structs (Managed)

value class
value struct

No

Defines a CLR value class

Classes and Structs (Managed)

interface class
interface struct

No

Defines a CLR interface

interface class

enum class
enum struct

No

Defines a CLR enumeration

enum class

property

Yes

Defines a CLR property

property

delegate

Yes

Defines a CLR delegate

delegate

event

Yes

Defines a CLR event

event (Visual C++)

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

abstract

Yes

Indicates functions or classes are abstract

abstract (Visual C++)

new

No

Indicates that a function is not an override of a base class version

new (new slot in vtable)

override

Yes

Indicates that a method must be an override of a base-class version

override

sealed

Yes

Prevents classes from being used as base classes

sealed

Keywords for Generics

The following keywords have been added to support generic types. For more information, see Generics (Visual C++).

Keyword

Context sensitive

Purpose

generic

No

Defines a generic type

where

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

finally

Yes

Indicates default exception handlings behavior

Exception Handling under /clr

for each

No

Enumerate elements of a collection.

for each, in

gcnew

No

Allocates types on the garbage-collected heap. Use instead of new and delete.

gcnew

initonly

Yes

Indicates a member can only be initialized at declaration or in a static constructor

initonly

literal

Yes

Creates a literal variable

literal (Visual C++)

nullptr

No

Indicates that a handle or pointer does not point at an object

nullptr

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

array

Type for representing CLR arrays

array (Visual C++)

interior_ptr

Points to data inside reference types.

interior_ptr

pin_ptr

Points to CLR reference types to temporarily suppress the garbage collection system

pin_ptr

safe_cast

Determines and executes the optimal casting method for CLR types

safe_cast

typeid

Retrieves a System.Type object describing the given type or object

typeid

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

^ (Handle to Object on Managed Heap)

%

Indicates a tracking reference

% (Tracking Reference)

More CLR Constructs

In addition to keywords and language constructs, this reference section includes the following CLR programming constructs.

Topic

Description

__identifier

Enables the use of keywords as identifiers

... (Variable Argument Lists)

Enables a function to take a variable number of arguments.

.NET Framework Equivalents to C++ Native Types (C++/CLI)

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

C-Style Casts with /clr

Describes how C-style casts are interpreted

__clrcall calling convention

Indicates the CLR-compliant calling convention

__cplusplus_cli

Predefined Macros

Custom Attributes (C++)

Describes how to define your own CLR attributes.

Exception Handling under /clr

Provides an overview of exception handling

Explicit Overrides

Demonstrates how member functions can override arbitrary members

Friend Assemblies (C++)

Discusses how a client assembly can access all types in an assembly component.

Implicit Boxing

Demonstrates the conditions where values types are boxed

Compiler Support for Type Traits

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

Reflection (C++/CLI)

Demonstrates the CLR version of run-time type information

System::String Handling in Visual C++

Discusses compiler conversion of string literals to String.

Type Forwarding

Allows you to move a type in a shipping assembly to another assembly, such that, client code does not have to be recompiled.

User-Defined Attributes

Demonstrates use-defined attributes

#using Directive (C/C++)

Imports external assemblies

XML Documentation (Visual C++)

Explains XML-based code documentation using /doc (Process Documentation Comments) (C/C++)

See Also

Other Resources

.NET Programming Guide

Native and .NET Interoperability