Export (0) Print
Expand All

Fundamentals of Managed Types

Visual Studio .NET 2003

Managed types provide support for features of the common language runtime and are subject to the advantages and restrictions of the runtime. This topic discusses the two fundamental categories of managed types:

  • GC types.
  • Value types.

This topic also discusses the use of the class and struct keywords with managed types.

GC Types

A significant benefit of the common language runtime is garbage collection, which destroys objects automatically when they are no longer needed. Managed types that support automatic garbage collection are called garbage-collected, or GC, types. GC types are defined with the __gc keyword:

// compile with: /clr
__gc class ExampleGCType
{
};

Objects based on GC types do not have to be deleted explicitly. Instead, they are disposed of or collected automatically when all pointers or references to that object go out of scope:

// compile with: /clr
void Func()
{
   ExampleGCType* ptr = new ExampleGCType();
}

Once this function returns, the instance of ExampleGCType is marked for destruction, because the only pointer to the object is now out of scope.

Note that the delete operator is not used, and yet no memory leak results. You can use the delete operator with GC types, but only for objects based on a GC type that explicitly defines a destructor. The delete operator calls the destructor but does not cause the object to be destroyed. Only the garbage collection system disposes of GC types.

The common language runtime provides the necessary support for garbage collection, but there are limitations. GC types can only be instantiated dynamically; they cannot be used as global or local variables. Attempting to do so results in a compiler error.

For information on managed type restrictions, see Restrictions of Managed Types.

Value Types

For global and local data types, the common language runtime provides support for value types, which are defined using the __value keyword:

// compile with: /clr
__value struct ExampleValueType
{
};

Objects based on value types cannot be created using the new operator. They must be created as a local or global variable:

// compile with: /clr
ExampleValueType globalVar;
void Func()
{
   ExampleValueType localVar;
}

Although value types cannot be created using the new operator, they can be allocated dynamically if the unmanaged memory heap is used. This is indicated using the __nogc keyword:

// compile with: /clr
void Func()
{
   ExampleValueType* pVar = __nogc new ExampleValueType;
}  // This results in a memory leak.

While this works, it forfeits garbage collection support, because the object is created on the C++ heap as opposed to the garbage collected heap. Therefore, the code above results in a memory leak. Nevertheless, this is sometimes necessary for mixed-mode, or interoperability, programming, where some elements are managed and others are unmanaged. For details, see Data Marshaling. The __nogc version of new cannot be used to instantiate GC types.

For information on managed type restrictions, see Restrictions of Managed Types.

class and struct Keywords

Unlike C#, which uses the class keyword to denote GC types and the struct keyword for value types, the meaning of the class and struct keywords remain the same as ANSI C++ when using Managed Extensions, except for the default access permissions of their members:

// compile with: /clr
__gc struct GCStructure
{
   Int32 member;
};
__value class ValueClass
{
   Int32 member;
};
void Func2()
{
   GCStructure* p = new GCStructure;
   p->member = 0;
   ValueClass vc;
   // The vc.member is private
}

Nevertheless, GC types are generally defined using the class keyword, because GC types tend to be large, complex objects, whereas value types are generally defined using the struct keyword, because they tend to be small, simple, objects with public data members. This is appropriate for use in the scope of a single function call.

Example

See ValueTypes Sample

See Also

Managed Types | Restrictions of Managed Types

Show:
© 2014 Microsoft