2009-07-06

Preparation for 3rd meeting on C#iD: Nullables

Once again: There are still lots of things I do not know about the CLR. I find it more shocking on each occasion.

  • I was not aware of the GetValueOrDefault method on Nullable. Never used it.
  • Boxing/Unboxing of Nullables: 4.2.2 pp 118, 119: I had no idea that this might be an issue. But that boxing an instance of Nullable results in either a null reference or a boxed value of the underlying type does make perfect sense.
  • The class Nullable. Never used it as well. But I should have used the GetUnderlyingType method. I firmly believe that I have implemented exactly that feature two or three times in the last couple of years in various project using reflection.
  • No supprise about the null coalescing operator. But I notice that it is one of the lesser known features of C#. My estimation is that 2 out of 3 devs do not know it.
  • Using Nullable for trying an operation: That idea is brilliant. Why didn't I thing of it? I used it right away.
  • Combining Comparers by using Nullable as the return type and than applying the null coalescing operator is a very cool thing especially regarding the readability/maintainability of the code.
Regarding the last topic I was thinking on how we would implement to allow the end user to create a sort expression by combining individual comparers. That still does not get any better with the solution presented in C#iD (to be fair: it never claimed to solve such a request, but I find it still interesting).
My first idea was to have each comparer have a reference to a NextComparer to which it would delegate if it's own result would be 0 (zero). That would allow us to chain together comparer in any sequence we like at runtime. It would be something like a Chain of Responsibility.
What I do not like about it is the fact that now the Comparer have to compare and have to handle the chaining as well. So I think having Decorator for the comparer that will take care of the chainging, the delegating to the next element of the chain and will decide how to handle the end of the chain would be much better.

No comments:

Whos.amung.us?

whos online

Ralf"s shared items