Cannot convert type: why is it necesssary to cast twice?

后端 未结 4 1904
南旧
南旧 2020-12-10 01:13

Given this highly simplified example:

abstract class Animal { }

class Dog : Animal
{
  public void Bark() { }
}
class Cat : Animal
{
  public void Mew() { }         


        
4条回答
  •  一生所求
    2020-12-10 01:48

    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.

提交回复
热议问题