I\'ve just upgraded from VS 2010 to 2015. I like the new null-conditional operator which is also known as null-propagation. This enables to simplify your code, for example:
If you use this code, and hover over x
, you see that x
is a int?
:
var x = text?.IndexOf("Foo", StringComparison.CurrentCultureIgnoreCase);
bool contains = x >= 0;
So the typing is still correct.
Then lets look into x >= 0
: that is a int? >= int
. Appearantly there is an operator between the nullable and non-nullable structs. That is why it works.
If you look at the IL, you will see it actually calls HasValue
and GetValueOrDefault()
. I guess there is an operator doing this, but I couldn't find it in the reference source, so it should be in the CLR or compiler:
instance !0 valuetype [mscorlib]System.Nullable`1::GetValueOrDefault()
...
instance bool valuetype [mscorlib]System.Nullable`1::get_HasValue()