The constructor of the derived class is called before the constructor of the base class

Well, initially I had several constants (e.g. MAX_SPEED) with different values ​​in each of the derived classes. The idea was to use these values ​​in some methods of the base class. This is when I realized that I cannot do this with constants, so I created read-only properties.

I need a method to assign these values ​​to private fields at the time of creation, preferably in the base class . But first, I have to evaluate the initial values ​​in the derived classes. Since these are properties, I could not find a way to initialize them in the definition, so the only way to do this is in derived constructors.

What the problem is: the values ​​are initialized after they are assigned to private fields in the base class. The solution I'm leaving with is to create a virtual method and assign there.

Is there a way to call the base constructor from the derived class so that the code from the derived constructor is called first?

class BaseClass { public BaseClass() { System.Console.WriteLine("This should be shown after"); } } class DerivedClass : BaseClass { public DerivedClass() : base() { System.Console.WriteLine("This should be shown first"); } } 

Of course, in the example, this will work the other way around. Is there a solution?

+3
source share
3 answers

No. The constructor of the base class is always executed before the body of the constructor of the derived class. But:

  • Initializers of instance variables in a derived class are executed before the constructor of the base class
  • The base class constructor can execute virtual methods that can be overridden in a derived class. This is almost always a bad idea. (At this point, all conditional conditions are invalid. You can observe readonly variables that are not set yet, because they will be set in the body of the constructor, for example. Ick.)

To demonstrate both of them:

 using System; class BaseClass { public BaseClass() { VirtualMethod(); Console.WriteLine("BaseClass ctor body"); } public virtual void VirtualMethod() { Console.WriteLine("BaseClass.VirtualMethod"); } } class DerivedClass : BaseClass { int ignored = ExecuteSomeCode(); public DerivedClass() : base() { Console.WriteLine("DerivedClass ctor body"); } static int ExecuteSomeCode() { Console.WriteLine("Method called from initializer"); return 5; } public override void VirtualMethod() { Console.WriteLine("DerivedClass.VirtualMethod"); } } class Test { static void Main() { new DerivedClass(); } } 

Output:

 Method called from initializer DerivedClass.VirtualMethod BaseClass ctor body DerivedClass ctor body 

Also, if your base class constructor takes a parameter, you can execute some code in the derived class to provide an argument:

 DerivedClass() : base(SomeStaticMethod()) 
However, they are all pretty smelly. What is your specific situation?
+12
source

No, you cannot do this. Base classes are initialized first. However, you can do something like this:

 class BaseClass { public BaseClass() { this.Initialize(); } protected virtual void Initialize() { System.Console.WriteLine("This should be shown after"); } } class DerivedClass : BaseClass { public DerivedClass() : base() { } protected override void Initialize() { System.Console.WriteLine("This should be shown first"); base.Initialize(); } } 
+8
source

Another option is to make the constructor of the child class static so that it first executes the constructor of the parent class. but not preferable it violates the oop design

0
source

Source: https://habr.com/ru/post/1232507/


All Articles