The only difference is in how the compiler checks that the casts are allowed.
In the first variant the compiler checks how Lion
can be cast to Animal
, then it checks how Animal
can be cast to IAnimal
. As both cast can be done safely as Lion
is an Animal
and Animal
is an IAnimal
, the compiler generates no code for casting at all, it's just an assignment.
In the second variant the compiler checks how Lion
can be cast to IAnimal
, then it checks if IAnimal
is the same as IAnimal
. As the cast can be done safely it generates no code for casting there either, it's also just an assignment.
As Lion
is an IAnimal
you don't need to do any casting at all, you can just assign it to the variable:
IAnimal animal3 = lion;
In that case the compiler will check how Lion
can be cast to IAnimal
, and as that can be done safely it doesn't generate any code for casting, just the assignment.