The Null Coalescing Operator, or Why I Don't Get Out Much
So I was sorting through some code working on some framework-like expressions and ran across the C# operator ?? I hadn't really run into that much prior to this point in time, so the reaction that I had to that was definitely "????????"
One of the initial problems this presented me with was "how do you Google on that to obtain any kind of rational result?" After being unable to answer that question rationally, I left my screen and went to get a beverage. Finally, since I couldn't find it in any operator reference I saw, I started thinking back to the nullable types released with C# 2.0. That was my first clue. That led me to the definition of the null coalescing operator, which can be found here.
This led to all sorts of fun code examples like:
int? z = x ?? y;
Since I am an avid abuser of the ternary operator in C# to populate default values in objects, this started to have instant appeal to me. You can define nullable objects that could potentially be null, but no, you don't want them to be. So you inject a value in for when they appear they possibly could be null.
In my mind, this is starting to grow infinite usage. In addition, it caused me to reflect upon why I don't get out much, and how this concept could be used to improve my social life. You see, my model previously was
TonightAction A = ActionPlans;
Yet "ActionPlans" for me frequently is null. Can you see the greatness yet? I just need to increase the usage of the null coalescing operator personally, so that I wind up with:
TonightAction A = ActionPlans ?? TakeWifeOnDate;
I bet you never knew the null coalescing operator could be of such value. Thanks Anders, et. al.
One of the initial problems this presented me with was "how do you Google on that to obtain any kind of rational result?" After being unable to answer that question rationally, I left my screen and went to get a beverage. Finally, since I couldn't find it in any operator reference I saw, I started thinking back to the nullable types released with C# 2.0. That was my first clue. That led me to the definition of the null coalescing operator, which can be found here.
This led to all sorts of fun code examples like:
int? z = x ?? y;
Since I am an avid abuser of the ternary operator in C# to populate default values in objects, this started to have instant appeal to me. You can define nullable objects that could potentially be null, but no, you don't want them to be. So you inject a value in for when they appear they possibly could be null.
In my mind, this is starting to grow infinite usage. In addition, it caused me to reflect upon why I don't get out much, and how this concept could be used to improve my social life. You see, my model previously was
TonightAction A = ActionPlans;
Yet "ActionPlans" for me frequently is null. Can you see the greatness yet? I just need to increase the usage of the null coalescing operator personally, so that I wind up with:
TonightAction A = ActionPlans ?? TakeWifeOnDate;
I bet you never knew the null coalescing operator could be of such value. Thanks Anders, et. al.



Comments