Override an overridden method (C#)

后端 未结 6 892
梦如初夏
梦如初夏 2021-02-03 17:47

I\'m trying to override an overridden method (if that makes sense!) in C#.

I have a scenario similar to the below, but when I have a breakpoint in the SampleMethod() in

6条回答
  •  不思量自难忘°
    2021-02-03 18:38

    The breakpoint is more than likely not being hit because you actually instantiated an instance of the "B" class.

    Method override resolution works based on the actual runtime type of the class whose method should be called. So, if you had the following code:

    C c = new C();
    c.SampleMethod();
    

    and the following:

    C c = new C();
    B b = (B)c;
    b.SampleMethod();
    

    both the runtime types of the class whose SampleMethod will be called is type B.

提交回复
热议问题