適用於 .NET 與 UWP 的元件擴充功能Component Extensions for .NET and UWP

C++ 標準讓編譯器廠商能夠為此語言提供非標準的擴充功能。The C++ standard allows compiler vendors to provide non-standard extensions to the language. Microsoft 提供擴充功能,可協助您將 C++ 機器碼連線至在 .NET Framework 或通用 Windows 平台 (UWP) 上執行的程式碼。Microsoft provides extensions to help you connect native C++ code to code that runs on the .NET Framework or the Universal Windows Platform (UWP). .NET 擴充功能稱為 C++/CLI,並會產生要在稱為 Common Language Runtime (CLR) 的 .NET 受控執行環境中執行的程式碼。The .NET extensions are called C++/CLI and produce code that executes in the .NET managed execution environment that is called the Common Language Runtime (CLR). UWP 擴充功能稱為 C++/CX,而它們會產生原生機器碼。The UWP extensions are called C++/CX and they produce native machine code.


對於新的應用程式,我們建議使用 C++/WinRT 而不是 C++/CX。For new applications, we recommend using C++/WinRT rather than C++/CX. C++/WinRT 是適用於 Windows 執行階段 API 的新標準 C++17 語言投影。C++/WinRT is a new, standard C++17 language projection for Windows Runtime APIs. 我們將繼續支援 C++/CX 和 WRL,但強烈建議讓新的應用程式使用 C++/WinRT。We will continue to support C++/CX and WRL, but highly recommend that new applications use C++/WinRT. 如需詳細資訊,請參閱 c + +/WinRTFor more information, see C++/WinRT.

兩種執行階段,一組擴充功能Two runtimes, one set of extensions

C++/CLI 會擴充 ISO/ANSI C++ 標準,並根據 Ecma C++/CLI 標準來定義。C++/CLI extends the ISO/ANSI C++ standard, and is defined under the Ecma C++/CLI Standard. 如需詳細資訊,請參閱以 C++/CLI 進行 .NET 程式設計 (Visual C++)For more information, see .NET Programming with C++/CLI (Visual C++).

C++/CX 擴充功能為 C++/CLI 的子集。The C++/CX extensions are a subset of C++/CLI. 雖然在大部分情況下擴充功能語法會完全相同,但產生的程式碼取決於您是否指定 /ZW 編譯器選項以將目標設定為 UWP,或指定 /clr 選項以將目標設定為 .NET。Although the extension syntax is identical in most cases, the code that is generated depends on whether you specify the /ZW compiler option to target UWP, or the /clr option to target .NET. 當您使用 Visual Studio 以建立專案時,會自動設定這些參數。These switches are set automatically when you use Visual Studio to create a project.

資料類型關鍵字Data Type Keywords

語言擴充功能包含「彙總關鍵字」,這類關鍵字會由空白字元分隔的兩個語彙基元所組成。The language extensions include aggregate keywords, which consist of two tokens separated by white space. 當語彙基元分開使用時,可能有一個意義,當它們一起使用時,則有另一個意義。The tokens might have one meaning when they are used separately, and another meaning when they are used together. 例如,"ref" 這個字是一般的識別項,而 "class" 這個字是宣告原生類別的關鍵字。For example, the word "ref" is an ordinary identifier, and the word "class" is a keyword that declares a native class. 但是,當這些字組合併以形成 ref class 時,產生的彙總關鍵字會宣告實體,也就是「執行階段類別」。But when these words are combined to form ref class, the resulting aggregate keyword declares an entity that is known as a runtime class.

擴充功能也包括「內容關鍵性」關鍵字。The extensions also include context-sensitive keywords. 關鍵字會被視為視內容而有所區別,取決於包含它的陳述式類型,以及它在該陳述式中的位置。A keyword is treated as context-sensitive depending on the kind of statement that contains it, and its placement in that statement. 例如,語彙基元 "property" 可以是識別碼,或者它可以宣告一種特殊的公用類別成員。For example, the token "property" can be an identifier, or it can declare a special kind of public class member.

下表列出在 C++ 語言擴充功能中的關鍵字。The following table lists keywords in the C++ language extension.

關鍵字Keyword 視內容而有所區別Context sensitive 目的Purpose 參考Reference
ref classref class

ref structref struct
No 宣告類型。Declares a class. 類別和結構Classes and Structs
value classvalue class

value structvalue struct
No 宣告值類型。Declares a value class. 類別和結構Classes and Structs
介面類別interface class

interface structinterface struct
No 宣告介面。Declares an interface. 介面類別interface class
enum 類別enum class

enum structenum struct
No 宣告列舉。Declares an enumeration. enum 類別enum class
property Yes 宣告屬性。Declares a property. propertyproperty
delegatedelegate Yes 宣告委派。Declares a delegate. 委派 (C++/CLI 和 C++/CX)delegate (C++/CLI and C++/CX)
eventevent Yes 宣告事件。Declares an event. eventevent

覆寫規範Override Specifiers

您可以使用下列關鍵字限定衍生的覆寫行為。You can use the following keywords to qualify override behavior for derivation. 雖然 new 關鍵字不是 c + + 的延伸模組,但它會列在這裡,因為它可以用於其他內容中。Although the new keyword is not an extension of C++, it is listed here because it can be used in an additional context. 某些規範對於原生程式設計也有效。Some specifiers are also valid for native programming. 如需詳細資訊,請參閱 如何:在原生編譯中宣告覆寫規範 (c + +/cli) For more information, see How to: Declare Override Specifiers in Native Compilations (C++/CLI).

關鍵字Keyword 視內容而有所區別Context Sensitive 目的Purpose 參考Reference
抽象abstract Yes 表示函式或類別為抽象。Indicates that functions or classes are abstract. 抽象abstract
new No 表示函式不是基底類別版本的覆寫。Indicates that a function is not an override of a base class version. new (vtable 中的新位置)new (new slot in vtable)
overrideoverride Yes 表示方法必須是基底類別版本的覆寫。Indicates that a method must be an override of a base-class version. overrideoverride
sealedsealed Yes 避免將類別做為基底類別。Prevents classes from being used as base classes. sealedsealed

泛型的關鍵字Keywords for Generics

下列關鍵字已加入以支援泛型類型。The following keywords have been added to support generic types. 如需詳細資訊,請參閱泛型For more information, see Generics.

關鍵字Keyword 視內容而有所區別Context sensitive 目的Purpose
通用generic No 宣告泛型類型。Declares a generic type.
wherewhere Yes 指定套用至泛型類型參數的條件約束。Specifies the constraints that are applied to a generic type parameter.

其他關鍵字Miscellaneous Keywords

下列關鍵字已加入至 C++ 擴充功能。The following keywords have been added to the C++ extensions.

關鍵字Keyword 視內容而有所區別Context sensitive 目的Purpose 參考Reference
最後finally Yes 表示預設例外狀況處理行為。Indicates default exception handlings behavior. 例外狀況處理Exception Handling
for each, infor each, in No 列舉集合的項目。Enumerates elements of a collection. for each, infor each, in
gcnewgcnew No 在記憶體回收堆積上配置類型。Allocates types on the garbage-collected heap. 使用取代 newdeleteUse instead of new and delete. ref new、gcnewref new, gcnew
ref newref new Yes 配置 Windows 執行階段型別。Allocates a Windows Runtime type. 使用取代 newdeleteUse instead of new and delete. ref new、gcnewref new, gcnew
initonlyinitonly Yes 表示成員只能在宣告或靜態建構函式中初始化。Indicates that a member can only be initialized at declaration or in a static constructor. initonly (c + +/CLI) initonly (C++/CLI)
literalliteral Yes 建立常值變數。Creates a literal variable. literalliteral
nullptr No 指出控制代碼或指標未指向物件。Indicates that a handle or pointer does not point at an object. nullptrnullptr

範本建構Template Constructs

下列語言建構會實作為範本,而不是關鍵字。The following language constructs are implemented as templates, instead of as keywords. 如果您指定 /ZW 編譯器選項,則它們會在 lang 命名空間中定義。If you specify the /ZW compiler option, they are defined in the lang namespace. 如果您指定 /clr 編譯器選項,則它們會在 cli 命名空間中定義。If you specify the /clr compiler option, they are defined in the cli namespace.

關鍵字Keyword 目的Purpose 參考Reference
arrayarray 宣告陣列。Declares an array. 陣列Arrays
interior_ptrinterior_ptr (只有 CLR) 指向參考類型中的資料。(CLR only) Points to data in a reference type. interior_ptr (C++/CLI)interior_ptr (C++/CLI)
pin_ptrpin_ptr (只有 CLR) 指向 CLR 參考類型以暫時隱藏記憶體回收系統。(CLR only) Points to CLR reference types to temporarily suppress the garbage-collection system. pin_ptr (C++/CLI)pin_ptr (C++/CLI)
safe_castsafe_cast 決定並執行執行階段類型的最佳轉型方法。Determines and executes the optimal casting method for a runtime type. safe_castsafe_cast
typeid (只有 CLR) 擷取 System.Type 物件,該物件描述指定類型或物件。(CLR only) Retrieves a System.Type object that describes the given type or object. typeidtypeid


下列類型宣告子會指示執行階段自動管理配置物件的存留期和刪除。The following type declarators instruct the runtime to automatically manage the lifetime and deletion of allocated objects.

運算子Operator 用途Purpose 參考Reference
^ 宣告物件的控制代碼;也就是,一個指向 Windows 執行階段或 CLR 物件的指標,不再使用它時即會自動刪除。Declares a handle to an object; that is, a pointer to a Windows Runtime or CLR object that is automatically deleted when it is no longer usable. 物件運算子的控制碼 (^) Handle to Object Operator (^)
% 宣告追蹤參考;也就是,Windows 執行階段或 CLR 物件的參考,不再使用它時即會自動刪除。Declares a tracking reference; that is, a reference to a Windows Runtime or CLR object that is automatically deleted when it is no longer usable. 追蹤參考運算子Tracking Reference Operator

本節列出與 CLR 有關的其他程式設計建構和主題。This section lists additional programming constructs, and topics that pertain to the CLR.

主題Topic 說明Description
__identifier (C++/CLI)__identifier (C++/CLI) (Windows 執行階段和 CLR) 可將關鍵字當作識別碼使用。(Windows Runtime and CLR) Enables the use of keywords as identifiers.
變數引數清單 (...) (C++/CLI)Variable Argument Lists (...) (C++/CLI) (Windows 執行階段和 CLR) 讓函式能夠接受可變數目的引數。(Windows Runtime and CLR) Enables a function to take a variable number of arguments.
C + + 原生類型的 .NET Framework 對應 (c + +/CLI) .NET Framework Equivalents to C++ Native Types (C++/CLI) 列出用來取代 C++ 整數類型的 CLR 類型。Lists the CLR types that are used in place of C++ integral types.
appdomain __declspec 改 性 劑appdomain __declspec modifier __declspec 要求每個 appdomain 都有靜態和全域變數的修飾詞。__declspec modifier that mandates that static and global variables exist per appdomain.
使用/clr 的 c 樣式轉換 (c + +/CLI) C-Style Casts with /clr (C++/CLI) 描述 C-style 轉換的解譯方式。Describes how C-style casts are interpreted.
__clrcall 呼叫慣例__clrcall calling convention 表示 CLR 相容的呼叫慣例。Indicates the CLR-compliant calling convention.
__cplusplus_cli 預先定義的巨集Predefined Macros
自訂屬性Custom Attributes 描述如何定義您自己的 CLR 屬性。Describes how to define your own CLR attributes.
例外狀況處理Exception Handling 提供例外狀況處理的概觀。Provides an overview of exception handling.
明確覆寫Explicit Overrides 示範成員函式如何覆寫任意成員。Demonstrates how member functions can override arbitrary members.
(c + +) 的 Friend 元件 Friend Assemblies (C++) 討論用戶端組件如何存取組件元件中的所有類型。Discusses how a client assembly can access all types in an assembly component.
Box 處理Boxing 示範 boxed 處理值類型的條件。Demonstrates the conditions in which values types are boxed.
類型特性的編譯器支援Compiler Support for Type Traits 討論如何在編譯時偵測類型的特性。Discusses how to detect characteristics of types at compile time.
受控、非 受控 pragmamanaged, unmanaged pragmas 示範 Managed 和 Unmanaged 函式如何共存於相同的模組。Demonstrates how managed and unmanaged functions can co-exist in the same module.
進程 __declspec 改 性 劑process __declspec modifier __declspec 要求每個進程都有靜態和全域變數的修飾詞。__declspec modifier that mandates that static and global variables exist per process.
反映 (C++/CLI)Reflection (C++/CLI) 示範執行階段類型資訊的 CLR 版本。Demonstrates the CLR version of run-time type information.
StringString 討論字串常值到 String 的編譯器轉換。Discusses compiler conversion of string literals to String.
型別轉送 (C++/CLI)Type Forwarding (C++/CLI) 在傳送組件中讓類型移動到另一個組件,讓用戶端程式碼不需要重新編譯。Enables the movement of a type in a shipping assembly to another assembly so that client code does not have to be recompiled.
使用者定義屬性User-Defined Attributes 示範使用者定義屬性。Demonstrates user-defined attributes.
#using 指示詞#using Directive 匯入外部組件。Imports external assemblies.
XML 檔XML Documentation 使用 /doc (處理文件註解) (C/C++) 來說明 XML 型程式碼文件Explains XML-based code documentation by using /doc (Process Documentation Comments) (C/C++)

請參閱See also

使用 c + +/CLI 進行 .NET 程式設計 (Visual C++) .NET Programming with C++/CLI (Visual C++)
原生和 .NET 互通性Native and .NET Interoperability