The statements of C++/CLI are the statements of C++. The language adds one loop, one cast, one clause of try, one way to ask for a type and the attribute syntax.
Quick reference
| Form | Meaning |
|---|---|
for each (T x in expr) stmt | runs stmt for each element of an array or a collection (IEnumerable, or a type with GetEnumerator) |
safe_cast<T>(x) | converts, unboxes or boxes, and throws System::InvalidCastException when the conversion is not possible |
dynamic_cast<T^>(h) | converts a handle, or gives nullptr when the object is not a T |
static_cast, const_cast, reinterpret_cast | as in C++, on managed types where they make sense |
try { } catch (T^ e) { } finally { } | managed exceptions are objects of a class that derives from System::Exception, thrown and caught by handle |
throw gcnew T(args);, throw; | throw a new exception, or rethrow the one being handled |
T::typeid, x->GetType() | the System::Type^ of a managed type or object |
[Attribute(args)] | a custom attribute before a declaration; [assembly: …]; for the assembly |
for each
array<String^>^ names = gcnew array<String^>{ "a", "b" };
for each (String^ s in names)
Console::WriteLine(s);
List<int>^ list = gcnew List<int>();
for each (int n in list) { }
for each and in are context-sensitive keywords. Elements can be changed (when the variable is a tracking reference) but not added or removed. Microsoft’s native C++ compiler accepts for each as well, for collections of the standard library; the range-based for of C++11 is preferred in native code.
Casts
Object^ o = list;
IEnumerable^ e = safe_cast<IEnumerable^>(o); // exception if it is not
List<int>^ l = dynamic_cast<List<int>^>(o); // nullptr if it is not
if (l != nullptr) { }
int i = safe_cast<int>(boxedObject); // unboxing
A C-style cast on handles is read by the compiler as one of the named casts (Microsoft has a page on C-style casts with /clr, which was not read), so safe_cast is the cast to write when a managed conversion is meant. Implicit boxing happens when a value goes where an Object^ is expected, as in Console::WriteLine("{0}", 5).
Exceptions
try {
throw gcnew ArgumentException("bad");
}
catch (ArgumentException^ e) {
Console::WriteLine(e->Message);
}
catch (Exception^) {
throw;
}
finally {
Console::WriteLine("always");
}
A managed exception is thrown by handle and caught by handle; a catch of the base class Exception^ catches all of them. finally is a context-sensitive keyword and runs whether or not an exception happened. Native C++ exceptions and structured exceptions (__try, __except) can be caught in the same function, and /clr implies /EHa, the exception model that catches both. Throwing a native object out of managed code makes the runtime wrap it.
Attributes
[assembly: System::Reflection::AssemblyVersion("1.0.0.0")];
[AttributeUsage(AttributeTargets::Class)]
public ref class MyAttribute : Attribute {
public:
property String^ Text;
};
[Serializable, My(Text = "x")]
public ref class Data {
[field: NonSerialized] int cache;
};
An attribute is a ref class that derives from System::Attribute; the suffix Attribute may be dropped where it is used. It goes in brackets before the declaration it describes, with arguments in parentheses and named arguments as Name = value; several attributes are separated by commas. The attribute target (assembly:, module:, type:, method:, field:, property:, event:, param:, return:) says what it applies to, and an assembly or module attribute ends in a semicolon. The viewer colours the target as a keyword and the attribute name as a type.
Not described
Known, not yet covered
- Reference: the rules of the managed
try(which native and managed exception types mix, the effect of/EHaon native destructors, and exceptions that leave a mixed-mode function) are given in outline. The pages ontypeid, reflection and thelockclass ofmsclrwere not read.