Override an overridden method (C#)

后端 未结 6 910
梦如初夏
梦如初夏 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:48

    That solution works fine; although to actually use it outside the class the method is in, you need to set the access of SampleMethod to public rather than protected in all of the cases it appears, so:

    public class A
    {
        public virtual void SampleMethod() 
        {
            Console.WriteLine("lol");
        }
    }
    
    public class B : A
    {
        public override void SampleMethod()
        {
            base.SampleMethod();
        }
    }
    
    public class C : B
    {
        public override void SampleMethod()
        {
            base.SampleMethod();
        }
    }
    

提交回复
热议问题