Creating an immutable value object in C# – Part IV – A class with a special value

Other posts:

  • Part I - Using a class
  • Part II - Making the class better
  • Part III - Using a struct
  • In the last post we looked at structs as ways to implement immutable value objects and we discovered that they have several issues.

    A good thing about the struct implementation was the introduction of an explicit 'special value' instead of 'null'. I personally like doing that because it forces me to think about what are the special values in my domain instead of blindly rely on null and its semantics. Plus, it also works where null breaks down, when there are multiple special values.

    Having explicit special values is obviously not related to structs in any way. You can use the same pattern with classes as well, but you have to manage 'null' values that can enter your API. Here is how I did it for the DateSpan class.

    First I defined an utility function to manage null values:

        public static void CheckNull<T>(T t) {
            if (t == null)
                throw new ArgumentNullException();

    Then I had to check my null precondition for each parameter of my API. I.E.

        public DateSpan Intersect(DateSpan other) {

    Most importantly I now have to check for null in 'Equals' and '==':

        public override bool Equals(object obj) {
            // Interesting choice, null is not valid in this domain
            if (this.GetType() != obj.GetType()) return false;
            DateSpan other = obj as DateSpan;
            return other.End == End && other.Start == Start;
        public static Boolean operator ==(DateSpan v1, DateSpan v2) {
            return (v1.Equals(v2));

    So now we have an immutable value object, represented as a class, with checks for nulls and a special value (not shown above because it is essentially the same as for structs). So, does this work?

    It does, but it is cumbersome to write. And if it is too cumbersome, I already know that I'm not going to use it. I might start doing it, but I would soon give up. Are there ways to ease the pain?

    One way would be to use snippets to help in writing the code. Snippets in this case have a couple of problems:

    • It is not easy to 'snippify' the logic inside 'Equals', 'GetHashcode' and such
    • It makes easy to write the code, but still it is hard to read it and maintain it

    In the next post we'll look at a better solution.

    Comments (6)

    1. Why are you ignoring the power of Guidance Automation to create all of the classes?????

    2. Oren Novotny says:

      Shouldn’t you simply return false for equality if the other value is null instead of throwing?  

    3. lucabol says:

      David: because I barely know what it is 😉

      Anyhow, I think there is a simpler solution. I’ll post it later.

      Onovotny: yes, that is usually the recommended way. In my case, I don’t want for these objects to ever be null. If one of them is null, it means that there is something wrong in my app and I want to know it. Hence the exception.

    4. Welcome to the thirty-eighth Community Convergence. These posts are designed to keep you in touch with

    5. For some reason, there’s been a lot of buzz lately around immutability in C#. If you’re interested in

    Skip to main content