© 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";
  t->SaySomething();
  
  System::Console::WriteLine(S"Goodnight, Moon");
}
Figure 2 New Managed C++ Keywords and Directives
Keyword
Description
__abstract
Declares a class that cannot be instantiated directly. Managed interfaces are intrinsically abstract
__box
Creates a reference type copy of a value type
__delegate
A "function pointer" that may be bound to an instance or static method
__event
A rallying point for delegate implementations interested in receiving callbacks
__finally
Declares a finally block
__gc
Declares a managed type
__identifier
Enables the use of a keyword as an identifier (necessary in a truly multi-language environment)
__interface
Declares a managed interface
__nogc
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
__pin
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
__property
Declares a property member for a managed class
__sealed
Prevents a method or class from being a base class. Value types are intrinsically sealed
__try_cast
Throws an exception of type System::IllegalCastException if the cast is illegal
#using
Brings in assembly metadata
__value
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 {
  public:
    String* Something;
    void SaySomething() {
        Console::WriteLine(Something);
    }
    ~Talker() {
        Console::WriteLine(S"~Talker");
    }
  };
}
Figure 4 Managed C++ Rosetta Stone
Managed Operation
C#
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] {...}
[System::Reflection::DefaultMemberAttribute
(S"Item")]
__gc class MyCollection {
__property String* get_Item(int index);
};
Referencing an assembly
/r:assembly.dll
#using <assembly.dll>
Bringing in a namespace
using System;
using namespace System;
Object variable
IFoo foo = new Foo();
IFoo* pFoo = new Foo();
Member access
foo.DoFoo();
pFoo->DoFoo();
Referencing a namespace
System.Console.WriteLine("");
System::Console::WriteLine("");
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 {...}
Show: