__nogc
Explicitly declares an unmanaged type.
__nogc class-specifier __nogc struct-specifier __nogc interface-specifier __nogc array-specifier __nogc pointer-specifier __nogc new
Remarks
The __nogc keyword is used to explicitly specify that an object is allocated on the standard C++ heap. This keyword is optional. If you don't specify __gc or __nogc in front of a class declaration, it defaults to __nogc.
Objects of this type are similar to standard C++ objects in that they are allocated from the standard C++ heap and are not subject to the restrictions of a managed object. For more information on __nogc, see 4.5.3 __gc and __nogc Keywords and Arrays and 16.2 __nogc Pointers in Managed Classes.
The __nogc keyword is also used when an object of a __value type is explicitly allocated on the standard C++ heap:
__value struct V { int i; };
int main()
{
V * v = __nogc new V;
v->i = 10;
return 0;
}
Note The __nogc keyword can also be applied to array and pointer types. For examples of the __nogc keyword, see 4.5.3 __gc and __nogc Keywords and Arrays.
A gc pointer cannot be a member of a __nogc class. See __value for guidelines on embedding a value type in a __nogc class.
Example
In the following example, an unmanaged class is declared (X) and an object is instantiated and modified:
// keyword__nogc.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;
__nogc class X
{
public:
int i;
};
int main()
{
X* x; // declares an unmanaged pointer of type X
x = new X(); // creates an unmanaged object of type X on the C++ heap
Console::WriteLine(x->i);
x->i = 4; // modifies the unmanaged object referred to
// by the unmanaged pointer x
Console::WriteLine(x->i);
delete x; // call C++ delete operator to clean up resource
return 0;
}
Sample Output
48378256 4
See Also
Managed Extensions for C++ Reference | C++ Keywords | __gc | __pin