Given this highly simplified example:
abstract class Animal { }
class Dog : Animal
{
public void Bark() { }
}
class Cat : Animal
{
public void Mew() { }
The problem is that the compiler can't guarantee that _animal can be casted to Dog since the only restriction you give the type parameter of SoundRecorded is that the type should be Animal OR inherit from Animal. So the compiler is practically thinking: what if you construct a SoundRecorder
, the cast operation then is invalid.
Unfortunatly (or not), the compiler isn't smart enough to see that you safely protected your code from ever reaching there by doing the 'is' check in advance.
If you were to store the given animal as an actual animal, this wouldn't be a problem since the compiler always allows any cast from a base type to a derived type. The compiler doesnt allow a cast from Dog to Cat though
EDIT See Jon Skeets answer for a more concrete explanation.