Another pointless operator for C#
I remember discovering C#'s null coalescing operator a while ago. I can't say I use it a lot, but it's useful to clean up my code when I do need it. However, there is another similar scenario that it doesn't cover:
SearchResult res = directorySearcher.FindOne();
return res != null ? res.GetDirectoryEntry() : null;
In this case I have my first object, res, and if it IS null, I want to return it - and if it isn't, I want to return a property of that object. In effect, it's the reverse of the ?? operator - return the first object if it is null, or the second object otherwise.
Can anyone think of a way of implementing such an operator without bothering the C# team over it? :)
EDIT: Here's a code sample of my proposed extension. Note the sneaky usage of the inverted question mark for the inverted operator.SearchResult res = directorySearcher.FindOne();
return res ¿¿ res.GetDirectoryEntry();
This, in the beautiful world inside my head, would return res.GetDirectoryEntry() if res is not null, but return null if it is. It relates to the first code sample like the way the two code samples in my original link do.