© 2004 Microsoft Corporation. All rights reserved.

Figure 1 Accessing the C# Talker Class
// talkercli.cpp

// Required for Managed C++ programs
#using <mscorlib.dll>

// Bring in the Talker class
#using <talker.dll>
using namespace MsdnMagSamples;

void main() {
  Talker* t = new Talker();
  t->Something = S"Hello, World";
  System::Console::WriteLine(S"Goodnight, Moon");
Figure 2 New Managed C++ Keywords and Directives
Declares a class that cannot be instantiated directly. Managed interfaces are intrinsically abstract
Creates a reference type copy of a value type
A "function pointer" that may be bound to an instance or static method
A rallying point for delegate implementations interested in receiving callbacks
Declares a finally block
Declares a managed type
Enables the use of a keyword as an identifier (necessary in a truly multi-language environment)
Declares a managed interface
Declares an unmanaged type or an unmanaged pointer to a managed type
#pragma managed
Declares a range of code to be managed, allowing access to managed types. This is on by default when a file is compiled using the /CLR option
#pragma unmanaged
Declares a range of code to be unmanaged, preventing access to managed types
Prevents a managed object from being moved by the garbage collector. This is sometimes necessary when you're calling unmanaged functions from within managed functions
Declares a property member for a managed class
Prevents a method or class from being a base class. Value types are intrinsically sealed
Throws an exception of type System::IllegalCastException if the cast is illegal
Brings in assembly metadata
Declares a managed value type
Figure 3 Managed C++ Talker Class
// talker.cpp

#using <mscorlib.dll>
using namespace System;

namespace MsdnMagSamples {
  public __gc class Talker {
    String* Something;
    void SaySomething() {
    ~Talker() {
Figure 4 Managed C++ Rosetta Stone
Managed Operation
Managed C++
Declaring an interface
interface IFoo {}
__gc __interface IFoo {};
Declaring a class
class Foo {}
__gc class Foo {};
Declaring a property
int x { get; set; }
__property int get_x();
__property void set_x(int x);
Implementing a property
int x { 
  get { return m_x; }
  set { m_x = x; }
__property int get_x() {
  return m_x;
__property void set_x(int x) {
  m_x = x;
Implementing an interface
class Foo : IFoo {}
class Foo : public IFoo {};
Declaring a delegate
delegate void CallMe();
__delegate void CallMe();
Declaring an indexer
String this[int index] {...}
__gc class MyCollection {
__property String* get_Item(int index);
Referencing an assembly
#using <assembly.dll>
Bringing in a namespace
using System;
using namespace System;
Object variable
IFoo foo = new Foo();
IFoo* pFoo = new Foo();
Member access
Referencing a namespace
Declaring an enum
enum Foo { bar, quux }
__value enum Foo { bar, quux };
Collision resolution
void IArtist.Draw() {...} 
void ICowboy.Draw() {...}
void IArtist::Draw() {...}
void ICowboy::Draw() {...}
Value type
struct Foo {...}
__value struct Foo {...};
Abstract type
abstract class Foo {...}
__abstract class Foo {...};
Sealed type
sealed class Foo {...}
__sealed class Foo {...};
C-style cast
try { IFoo foo = (IFoo)bar; }
catch( IllegialCastException
e) {...}
try { IFoo* pFoo = __try_cast<IFoo*>(pBar); }
catch( IllegialCastException* e )
Dynamic cast
IFoo foo = bar as IFoo;
If( foo != null ) ...
IFoo* pFoo = dynamic_cast<IFoo*>(pBar);
if(pFoo !=0)...
Type check
If( bar is IFoo ) ...
if( dynamic_cast<IFoo*>(pBar) != 0 ) ...
Exception handling
try {...}
catch(MyException e) {...}
finally {...}
try {...}
catch(MyException* pe) {...}
__finally {...}