A managed type has the members that the runtime has: fields, functions, properties, events, nested types and a static constructor. C++/CLI adds a form for each that C++ lacks.
Quick reference
| Member | Form |
|---|---|
| trivial property | property String^ Name; (the compiler writes the field and the accessors) |
| property block | property int Size { int get(); void set(int v); }; omit one accessor for a read-only or write-only property |
| indexed property | property T default[int] { T get(int i); void set(int i, T v); }; a name instead of default makes a named indexer |
| static property | static property int Count { int get(); } |
| trivial event | event EventHandler^ Changed; |
| event block | event D^ E { void add(D^ d); void remove(D^ d); void raise(Object^ s, EventArgs^ e); } |
| delegate | delegate void Notify(Object^ sender, EventArgs^ e); |
| constant field | literal int Sides = 4;: a compile-time constant, in metadata |
| read-only field | initonly static int Count = 0;: set where declared or in the (static) constructor |
| static constructor | static Name() { … } |
| operator | static Name^ operator+(Name^ a, Name^ b); a ref class defines operators as static functions |
| generic type or method | generic<typename T> where T : IComparable<T> before the declaration |
Properties
public ref class Account {
int balance;
public:
property String^ Owner; // trivial
property int Balance { // block
int get() { return balance; }
void set(int value) { balance = value; }
}
property int default[int] { // indexer
int get(int i) { return i * balance; }
}
};
A property is called with the member syntax, a->Balance = 5;, and compiles to the two methods get_Balance and set_Balance with a metadata property that joins them. The accessors may have different access labels. A property can be virtual, abstract or in an interface.
Events and delegates
public delegate void Notify(Object^ sender, EventArgs^ e);
public ref class Button {
public:
event Notify^ Clicked;
void Fire() { Clicked(this, EventArgs::Empty); }
};
public ref class Form {
void OnClick(Object^ s, EventArgs^ e) { }
public:
Form() {
Button^ b = gcnew Button;
b->Clicked += gcnew Notify(this, &Form::OnClick);
}
};
A delegate type derives from System::MulticastDelegate. A delegate is built with gcnew D(object, &Class::Method) for an instance method and gcnew D(&Class::Method) for a static one, and is called like a function or with Invoke. An event holds a list of delegates; += adds one and -= removes one, through the add and remove accessors. In a trivial event the compiler writes the three accessors and the field. Only the class that declares an event can raise it.
Override specifiers and explicit overrides
| Specifier | Meaning |
|---|---|
override | the function must override a virtual function of a base class: an error if none exists |
new | the function does not override: it hides the base function and takes a new slot in the vtable |
sealed | no derived class may override the function |
abstract | the function has no body here (equivalent to = 0) |
ref class Base { public: virtual void F() {} };
ref class Derived : Base {
public:
virtual void F() override sealed {}
virtual void G() = IShape::Draw {} // explicit override of an interface function
};
An explicit override (= Interface::Function) names the interface or base function that a function with a different name implements, which also lets one class implement two interface functions of the same name. The specifiers are also valid in native C++ compiled with /clr.
Generics
generic<typename T>
where T : IComparable<T>
public ref class Box {
T item;
public:
Box(T item) : item(item) {}
T Max(T other) { return item->CompareTo(other) >= 0 ? item : other; }
};
generic<typename K, typename V>
where K : IComparable<K>
where V : ref class, gcnew()
public ref class Table {};
generic<typename T> T Identity(T x) { return x; }
int n = Identity<int>(3);
A generic is a .NET generic: the runtime keeps the type parameter, so it is one type for every instantiation, visible to C# and Visual Basic, and filled in when the program runs. A constraint is written where T : constraint-list, one where clause for each type parameter, with these constraints: a base class, interfaces (comma-separated), ref class (a reference type), value class (a value type other than Nullable), gcnew() (a public constructor with no parameters), and another type parameter.
| Generics | C++ templates |
|---|---|
| kept in the metadata; filled in at run time; one type across assemblies | expanded at compile time; separate code for each instantiation; instances in different assemblies are different types |
| no non-type parameters, no specialisation (explicit or partial), no template-template parameters, the parameter cannot be a base class | all of these are allowed |
| callable from other .NET languages | only inside C++ |
A template can pass its parameter on to a generic, but a generic cannot pass its parameter on to a template, because the template needs it at compile time.
Not described
Known, not yet covered
- Reference: operator overloading rules for ref classes and value classes, conversion operators, static operator forms, generic interfaces and generic delegates, and the variance rules are not described. The
cliextcontainers (STL/CLR) are not described. - Viewer: a property or event block is listed with its type and name in the outline; its accessors are not listed separately.