The other day I got a code review comment saying that I should implement the full blown disposable pattern for IDisposable including the protected virtual void Dispose(bool disposing) method. For a long time I had implemented the IDisposable without caring about the additional overload - and I started worrying how many bugs I’ve introduced by not doing so.

The pattern

The most recent official documentation I could find describes the pattern in details, which I’ll summarize below.

Continue reading

The other day I wanted to use Newtonsoft JSON serializer to convert objects to JSON strings, with the twist that if some property of the object was null it should be defaulted to a specific value in the JSON string.

There is the DefaultValueAttribute but that seems to work only for deserialization (or so it seemed when I gave up searching for an out of the box behavior).

It turns out you can implement a ContractResolver. Inheriting from the base DefaultContractResolver seems to be the easiest way to go. Then you can override just the CreateProperty method and provide an instance of IValueProvider to the property contract generated by that method. Check out the example below.

Continue reading

Author's picture

Andre Esteve

Curious about this picture? These are Tulipa gesneriana, red tulips. This picture was taken on the northwest coast of the Washington state, and then a voronoi blur applied on it.