The Esoteric ?? Operator
(Aug 17 2007 - 11:49:10 AM
by Jonathan Carter
) - [print blog
So I was doing a little reflecting last night (as I sadly love to do in my free time) and I stumbled across something interesting. I was peering into the SetField<T> extension method in the DataRowExtensions class (which is part of the LINQ To DataSet assembly added with .NET 3.5) and noticed the following line of code:
row[column] = value ?? DBNull.Value;
I like to consider myself pretty competent with C#, but I had honestly never seen the ?? operator before, let alone ever heard anyone else talk about it. It is pretty clear what it is doing, and after seeing it I was blown away that something so simple and so beautiful existed in C# all this time and I didn't even know about it. So I started asking around to different colleagues and friends of mine, all of whom are extremely well versed in both .NET and C#, and none of them had ever heard of this operator. I then revisted the list of operators in the C# specification and low and behold, ?? was there.
Apparently I'm an idiot, and so are all of my friends. But all faults aside, I found a treasure today, and I owe it all to Lutz Roeder (developer of .NET Reflector) for providing the most dominant disassembler tool ever, and the LINQ To DataSet team, for representing this great operator in their code.