Strongly typing ID values in C#
<p>Is there a way to strongly type integer ID values in C#?</p>

<p>I've recently been playing with Haskell and can immediately see the advantages of its strong typing when applied to ID values, for example you would never want to use a <em>PersonId</em> in place of a <em>ProductId</em>.</p>

<p>Is there a nice way to create an <em>Id</em> class/struct that can be used to represent IDs of a given type?</p>

<p>I had the following idea but unfortunately it <strong>isn't legal</strong> on many levels. You can't have an abstract struct and the implicit/explicit cast operators wouldn't be inherited.</p>

<pre><code>public abstract struct Id
{
    int _value;

   public Id(int value)
   {
      _value = value;
   }

   // define implicit Id to int conversion operator:
   public static implicit operator int(Id id) 
   {
      return _value;    
   }

   // define explicit int to Id conversion operator:
   public static explicit operator Id(int value) 
   {
      return new Id(value);
   }

   public bool Equals(object obj)
   {
      if(GetType() == obj.GetType()) 
      {
         Id other = (Id)obj;
         return other._value == _value;
      }
      return false;
   }

   public int GetHashCode()
   {
      return _value.GetHashCode();
   }
}

struct PersonId : Id { public PersonId(int value) : base(value) {} }
struct ProductId : Id { public ProductId(int value) : base(value) {} }
</code></pre>

<p>Are there any valid ways to perform something similar?  How else can we prove that integer IDs type aren't being confused across a large application?</p>
