Partager via


__value 

Note

This topic applies only to version 1 of Managed Extensions for C++. This syntax should only be used to maintain version 1 code. See Classes and Structs (Managed) for information on using the equivalent functionality in the new syntax.

Declares a class to be a __value type.

__value class-specifier
__value struct-specifier
__nogc array-specifier
__nogc pointer-specifier

Remarks

A __value type differs from __gc types in that __value type variables directly contain their data, whereas managed variables point to their data, which is stored on the common language runtime heap.

The following conditions apply to __value types:

  • The __value keyword cannot be applied to an interface.

  • A __value type can inherit from any number of interfaces and cannot inherit from other types or __value types.

  • A __value type is sealed by definition. For more information, see __sealed.

  • It is valid to use a __value type anywhere a managed type is allowed.

Note

The __value keyword is not allowed when used with the __abstract keyword.

A __value type can be explicitly connected to a System::Object pointer. This is known as boxing.

The following guidelines apply to embedding a value type inside a __nogc type:

  • The value type must have LayoutSequential or LayoutExplicit.

  • The value type can not have gc pointer members.

  • The value type can not have private data members.

In Managed Extensions for C++, the equivalents to a C# class and a C# struct are as follows:

Managed Extensions for C++ C# For more information

__gc struct

-or-

__gc class

class

class keyword

__value struct

-or-

__value class

struct

struct keyword

Example

In the following example, a __value type (V) is declared and then two instances of the __value type are manipulated:

// keyword__value.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>

__value struct V { 
   int m_i;
};

int main() {
   V v1, v2;
   v1.m_i = 5;
   v2 = v1;   // copies all fields of v1 to v2
   v2.m_i = 6;   // does not affect v1.m_I
}