The ?
is a nullable value type.
You can use the ??
operator to mix it with value types:
const int DefaultValue = -1;
// get a result that may be an integer, or may be null
int? myval = GetOptionalIdFromDB();
// the value after ?? is used if myval is null
int nonNullable = myval ?? DefaultValue;
The nullable type can be compared to null, so the above is shorthand for:
if( myval != null ) {
nonNullable = myval.Value;
} else {
nonNullable = DefaultValue;
}
But I prefer ??