title: Can I overload a "cast from null" operator in C#?
body: <p>I have a struct type in C#. I want to be able to convert <code>null</code> implicitly to this type. For instance, <code>null</code> could be represented by a special value of the struct type and the cast operator should return a struct with this value.</p>
  
  <p>In C++, I could use an implicit cast operator overload of type <code>std::nullptr_t</code>. Is there a comparable type in C#?</p>
  
  <p>I have had the idea to use a special <code>NullType</code> class which has no instances. This works but looks somehow ugly. Is there a better way?</p>
  
  <p>Example:</p>
  
  <pre><code>class NullType
  {
      private NullType(){} // ensures that no instance will ever be created
  }
  
  struct X
  {
      private static readonly int nullValue = -1;
      private int val;
  
      public X(int val){ this.val= val; }
  
      public static implicit operator X(NullType t)
      { 
          return new X(nullValue);
      }
  }
  
  class MainClass
  {
       public static void Main(string[] args)
       {
            X x = null; // Works now!
       }
  }
  </code></pre>