- Home /
Calling a parent constructor after the child's in C#
In UnityScript, you can call a parent's constructor whenever you want inside a child constructor with super(). It let's you process stuff in the child constructor before you call the parent constructor.
(How) can I do that in C# (and Boo) ?
The default behaviour calls the parent constructor before anything happens in the child constructor.
Thanks in advance.
Answer by simonmc · Apr 11, 2012 at 11:39 PM
The short answer is that you can't. C# assumes that a base class's constructor is absolutely necessary and must always be called before anything else.
A possible workaround, however, is to specify the argument to the base class constructor via a static method. That static method will then be called on grabbing arguments to the base class instructor so will happen first.
As an example:
public class Baser
{
public Baser(int a)
{
Debug.Log(a);
}
}
public class Deriver : Baser {
public Deriver(int a, int b) : base(Processor(a, b)) {
}
public static int Processor(int a, int b) {
Debug.Log(b);
return a;
}
}
Here, calling new Deriver(10, 20);
will result in the derived class calling it's Log function before the one in the base classes constructor, and so the output would be
20
10
Thanks, but since Processor() has to be static, you can't do non static stuff, like setting some members (which I need in my case).
$$anonymous$$y case was pretty simple, I just moved a call to a method from the parent constructor to the child's.
For anyone interested, in Boo, it works just like in UnityScript:
// UnityScript
class Deriver extends Baser {
function Deriver () {
// do stuff
super ();
}
}
// Boo
class Deriver (Baser):
def constructor ():
// do stuff
super ()
Answer by Bunny83 · Apr 12, 2012 at 10:36 AM
In C# base constructors are always called before any child constructor. CIL, the language C#, boo and UnityScript are based on, seems to support calling a chils constructor before the base constructor. However i don't see any good reason for doing that. You might want to overthink your class design. The base class shouldn't be dependend on the derived class.
Here's a way that works, but i wouldn't recommend it:
public class BaseClass
{
public BaseClass()
{
Debug.Log("BaseClass");
Init();
}
public virtual void Init()
{
Debug.Log("Base Init");
}
}
public class DerivedClass : BaseClass
{
public DerivedClass()
{
Debug.Log("DerivedClass");
}
public override void Init()
{
Debug.Log("Derived Init");
base.Init();
}
}
This will cause the following output:
BaseClass
DerivedClass
Derived Init
Base Init
Answer by unity_P1Hihy3g7bgP8A · Apr 10, 2018 at 08:43 AM
This code above gave me the below when doing 'new DerivedClass();'. Funny :) .net 4.5
BaseClass Derived Init Base Init DerivedClass
Your answer
Follow this Question
Related Questions
Can i set class vars automatically when calling class constructor with attributes? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Adding parameter to instance of a class during runtime 0 Answers
CSharp Classes = Scripts? Difference between private string and normal string? 1 Answer