Nerseus
Danner
Not much is said about the new ?? operator (maybe it's not an operator - who knows?) but it's there in C#. It's meant to work as a coallesce on nullable types, which can be declared with the new ? syntax.
For example:
The new syntax of "int?" declares a generic Nullable type, but easier. Most people know about this. What I just learned is that to return a "default" value of a null int, you can use the new ?? syntax. For example:
This says, if x is not null, assign the value to y. If it is null, use the righthand side, or 0 in this case. You can use any expression in place of 0, including a property, method call, etc. - including cascading ?? operations. For example:
-nerseus
For example:
C#:
int? x = null;
// Same as
Nullable<int> y = null;
The new syntax of "int?" declares a generic Nullable type, but easier. Most people know about this. What I just learned is that to return a "default" value of a null int, you can use the new ?? syntax. For example:
C#:
int? x = null;
int y = x ?? 0;
This says, if x is not null, assign the value to y. If it is null, use the righthand side, or 0 in this case. You can use any expression in place of 0, including a property, method call, etc. - including cascading ?? operations. For example:
C#:
int? x = null;
Nullable<int> y = null;
int z = x ?? y ?? 0; // Sets z to 0
y = 5;
int z = x ?? y ?? 0; // Sets z to 5
-nerseus