Why can't I create an abstract constructor on an abstract C# class?

后端 未结 8 2161
无人及你
无人及你 2020-11-28 08:08

I am creating an abstract class. I want each of my derived classes to be forced to implement a specific signature of constructor. As such, I did what I would have done has I

8条回答
  •  余生分开走
    2020-11-28 08:48

    not sure if this helps - but i feel this would be a solution to your problem:

    public class A
    {
      public A(int a, int b)
      {
        DoSomething(int a, int b);
      }
    
      virtual public void DoSomething(int a, int b)
      {
    
      }
    }
    
    public class B : A
    {
      override public void DoSomething(int a, int b)
      {
        //class specific stuff
      }
    }
    

    with the result that you can call a constructor with the required arguments in any derived class with the correct behaviour.

提交回复
热议问题