Wednesday 31 October 2007

Ignorance

For the past 3 years or so, I have been ignorant of something that I can do in C#. That is, use the a ?? b operator. This doesn't exist in C++ or C, but it is nice if you don't know about it. What is return a if it is not null, otherwise it returns b. Therefore, what was previously

return var == null ? default : var;

can now be written as

return var ?? default;

Which is nice and small. They both could also be written as

if(var == null) return default; return var;

or more verbosely as

if(var == null) {
  return default;
}
return var;

Of course, I should have discovered this 3 years ago, but I didn't, oops. Never mind, that's why, I suppose I should look at open source code, because then I would see things I didn't know about and be forced to learn them, instead of just stumbling across them. Well, I'd still be stumbling across them, just a lot faster than I am now.

No comments: