title: How to handle null when comparing equality of value objects?
body: <p><em>Note: I use C# as an example, but the problem is virtually the same in Java and probably many other languages.</em></p>
  
  <p>Assume you implement a value object (as in <a href="http://martinfowler.com/bliki/ValueObject.html" rel="noreferrer">value object pattern by M. Fowler</a>) and it has some nullable field:</p>
  
  <pre><code>class MyValueObject
  {   
      // Nullable field (with public access to keep the example short):
      public string MyField;
  }
  </code></pre>
  
  <p>Then, when overriding Equals(), how do you treat the case when both value objects have their MyField set to null? Are they equal or not?</p>
  
  <p>In C#, treating them as equal seems obvious, because:</p>
  
  <ol>
  <li><p>This is the behaviour of Equals() when you use a C# struct instead of a class and do not override Equals().</p></li>
  <li><p>The following expressions are true:</p>
  
  <pre><code>null == null
  object.ReferenceEquals(null, null)
  object.Equals(null, null)
  </code></pre></li>
  </ol>
  
  <p>However, in SQL (at least in SQL Server's dialect), <code>NULL = NULL</code> is false, whereas <code>NULL is NULL</code> is true.</p>
  
  <p>I am wondering what implementation is expected when using an O/R mapper (in my case, NHibernate). If you implement the "natural" C# equality semantics, may there be any ill effects when the O/R mapper maps them to the database?</p>
  
  <p>Or maybe allowing nullable fields in value objects is wrong anyway?</p>