System.Drawing.Point….. WTF

I had been using System.Drawing.Point as my means of storage for game coordinates until now. During my time catching up with unit tests I had a horrific realisation. Against all best practice information on structs in C# I found that Point is in fact a mutable struct.

I think I need to write that one more time so it can sink in…. Point is a MUTABLE struct.

Having a value type that is mutable is an oxymoron so all I can figure is that Point has been around since .NET V1 days when the advice was different and changing it now would break loads of existing code.

Given that I am aiming for fully immutable state this is catastrophic. I am just glad that I have caught this at this stage in development and not later down the line when a refactor to a different type would have been far more ugly and time consuming in nature. Even at this state without my friend ReSharper this could have been far worse than it needed to be.

So now I have a new Coordinate struct that is nice and safe and immutable. Shame that I was forced into this extra work because of the mess Microsoft had caused in their own core library but that is life at the code face 🙂

Interfaced structs causes boxing, or oh bugger

So after my work on interfaced struct based monads it looks like it was all in vain, as soon as you reference a struct via its interface it is boxed turning it into a reference type anyway. With this boxing I lose he benefits of the struct based solutions so it is time to accept this and just convert to a class based solution.

At least I now see why so many libraries opt for the class based approach. Oh to have F# style discriminated unions in C#.