Note Note

This topic applies only to version 1 of Managed Extensions for C++. This syntax should only be used to maintain version 1 code. In the new syntax, you do not need to explicitly mark a type as unmanaged.

Explicitly declares an unmanaged type.

__nogc class-specifier 
__nogc struct-specifier 
__nogc interface-specifier 
__nogc array-specifier 
__nogc pointer-specifier 
__nogc new

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.

The __nogc keyword is also used when an object of a __value type is explicitly allocated on the standard C++ heap:

// keyword__nogc.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>
__value struct V { 
   int i;
int main() {
   V * v = __nogc new V;
   v->i = 10;
   delete v;

The __nogc keyword can also be applied to array and pointer types.

A gc pointer cannot be a member of a __nogc class. See __value for guidelines on embedding a value type in a __nogc class.

In the following example, an unmanaged class is declared (X) and an object is instantiated and modified:

// keyword__nogc_2.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>
using namespace System;

__nogc class X {
   int i;

int main() {
   X* x;   // declares an unmanaged pointer of type X
   x = new X();   // creates unmanaged object of type X on the C++ heap

   x->i = 4;   // modifies unmanaged object

   delete x;   // call C++ delete operator to clean up resource

Community Additions