#1,153 – Different Delegate Types Are Not Assignment Compatible

Different delegate types are not assignment compatible, even if they define the same method signature.

For example, suppose we have two delegate types with the same signature and a method that matches the signature.

        public delegate void DogDelegate(Dog d);
        public delegate void DogDel2(Dog d);

        public static void UseDog(Dog d) 
            // do something with dog

Notice below that we can’t assign an instance of DogDelegate to a variable of type DogDel2, even if we use a cast.

            DogDelegate d1 = UseDog;

            // Another instance of same delegate type
            // is assignment compatible
            DogDelegate d2 = d1;  // works

            // Assignment to a different delegate type
            // is NOT allowed
            DogDel2 d3 = d1;  // Error: Can't convert

            // Cast doesn't work either
            DogDel2 d4 = (DogDel2)d1;  // Error