Non-Nullable Types
Similar to how value types can be made nullable in standard C# by suffixing the typename with a question mark (?
), RemObjects C# allows reference types – which are nullable by default – to be marked as not nullable by suffixing the type name with an exclamation point (!
).
This can make for more robust code, as variables, fields, properties or parameters declared as such can be relied on to not be null
. In many cases, the compiler can even emit compile-time warnings or errors, for example when passing a literal null
to a non-nullable parameter.
For consistency, the !
operator is alos allowed on value types, where it will be ignored, simliar to how the ?
is allowed on referene types (are nullable by default) and is ignored there.
Int32 i1; // non-nullable by default, 0
Button b1; // nullable by default, null
Int32! i1; // non-nullable by default, 0, same as above
Button? b1; // nullable by default, null, same as above
Int32? i2; // nullable, null
Button! b2 = new Button(); // not nullable, thus needs initialization
Please also refer to the Nullability topic in the Language Concepts section for more detailed coverage.