Why aren't reference types polymorphic?

Q: Why aren't reference types polymorphic?

A: Consider the following code:

 using System;
class Dog {
   public string Name;
}

class Test
{
   public static void Swap(ref object a, ref object b) {
      object temp;
      temp = a;
      a = b;
      b = temp;
   }

   public static void Main() {
      Dog d1 = new Dog();
      d1.Name = "fido";
      Dog d2 = new Dog();
      d2.Name = "rex";
      Swap(ref d1, ref d2);
   }
}

The compiler will report an error on the call to the Swap() function. Why? Consider if the swap function was like this:

    public static void Swap(ref object a, ref object b) {      a = 5;      b = “Hello“;
   }

If the compiler allowed this code, it would mean assigning a boxed int to a Dog object, which is clearly not type safe.

[Author: Eric Gunnerson]