Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lion · Apr 11, 2012 at 11:12 PM · c#classconstructor

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lion · Apr 12, 2012 at 09:29 AM 0
Share

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 ()

avatar image
1

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


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges