What is the difference between base reference type vs derived reference type

后端 未结 6 804
清酒与你
清酒与你 2021-01-07 13:20
class Dog
{
}
class BullDog : Dog
{
}
class Program
{
    static void Main()
    {
        Dog dog2 = new BullDog();
        BullDog dog3 = new BullDog();
    }
}
         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 13:48

    When you use the base type as the reference type, you can only call members that are defined on the base type. You need to cast in order to use the members of the supertype.

    So, if BullDog defined a DoNotRelease method, you could not call it from a Dog reference directly.

    In terms of var - it will infer the more specific type. So if using new BullDog() the inferred type will be BullDog.

提交回复
热议问题