IanG on Tap: Oh No! Not the TimedLock Again!: "Oh No! Not the TimedLock Again!"

This code shows how to do locks with timeouts and a way to check if someone forgot to call Dispose on a IDisposable struct.


C# Object Initializers. More than meets the eye

Up till today I liked C# object initializers because the looked nicer and when using parameter objects they even turn into something like named parameters.

But just a couple of minutes ago I was changing some test data creation code that didn't use object initializers but plain property assignments.

As I use ReSharper I alt-returned the assignments into an initializer. What struck me was that not all property assignments where put into the initializer.

Why? Because the same property had two assignments (of course with different values). I only noticed that when I was trying to incorporate the assignment into the initializer and the red squiglies from ReSharper. The C# compiler issues an error when you try to compile this type of code:
error CS1912: Duplicate initialization of member 'X'
So object initializers not only look better, they prevent you from making such a dumb mistake.


Cool Solutions: Ninotech Path Copy 4

Cool Solutions: Ninotech Path Copy 4

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.


whos online

Ralf"s shared items