title: Serialize DateTime as binary
body: <p>How do I properly serialize a DateTime object (e.g. using a BinaryWriter), and preserve its complete state?</p>
  
  <p>I was under the impression that a date time was only represented by an internal long integer, and that this integer was accessible as the <code>Ticks</code> property of the DateTime. However, looking at the implementation, the Ticks property actually returns a subset of the real internal data which is stored in an ulong called <code>dateData</code></p>
  
  <p>Ticks (which just gets InternalTicks) is implemented like so:</p>
  
  <pre><code>public long InternalTicks
  {
      get { return (long) this.dateData &amp; 4611686018427387903L; }
  }
  </code></pre>
  
  <p>As far as I can see this means the dateData may contain information that is not revealed by the <code>Ticks</code> property.</p>
  
  <p>Stranger yet, the BinaryFormatter Serialization of a DateTime does this in GetObjectData():</p>
  
  <pre><code>info.AddValue("ticks", this.InternalTicks);
  info.AddValue("dateData", this.dateData);
  </code></pre>
  
  <p>That will output <em>two</em> longs in the stream, where one of them would be easily recovererd from the other! </p>
  
  <p>How can I serialize my DateTime without risk of losing any of the internal state, (preferably of course in just 8 bytes, and without reflection). I'm thinking maybe it can be cast (unsafe), directly to an ulong?</p>
  
  <p>Or am I worrying for no reason, will the <code>Ticks</code> property actually encode all the necessary state? </p>