C# constructor execution order

后端 未结 7 843
渐次进展
渐次进展 2020-11-22 14:57

In C#, when you do

Class(Type param1, Type param2) : base(param1) 

is the constructor of the class executed first, and then the superclass

相关标签:
7条回答
  • 2020-11-22 15:36

    Not sure if this should be a comment/answer but for those who learn by example this fiddle illustrates the order as well: https://dotnetfiddle.net/kETPKP

    using System;
    
    // order is approximately
    /*
       1) most derived initializers first.
       2) most base constructors first (or top-level in constructor-stack first.)
    */
    public class Program
    {
        public static void Main()
        {
            var d = new D();
        }
    }
    
    public class A
    {
        public readonly C ac = new C("A");
    
        public A()
        {
            Console.WriteLine("A");
        }
        public A(string x) : this()
        {
            Console.WriteLine("A got " + x);
        }
    }
    
    public class B : A
    {
        public readonly C bc = new C("B");
    
        public B(): base()
        {
            Console.WriteLine("B");
        }
        public B(string x): base(x)
        {
            Console.WriteLine("B got " + x);
        }
    }
    
    public class D : B
    {
        public readonly C dc = new C("D");
    
        public D(): this("ha")
        {
            Console.WriteLine("D");
        }
        public D(string x) : base(x)
        {
            Console.WriteLine("D got " + x);
        }
    }
    
    public class C
    {
        public C(string caller)
        {
            Console.WriteLine(caller + "'s C.");
        }
    }
    

    Result:

    D's C.
    B's C.
    A's C.
    A
    A got ha
    B got ha
    D got ha
    D
    
    0 讨论(0)
  • 2020-11-22 15:39

    The constructor of the baseclass is called first.

    0 讨论(0)
  • 2020-11-22 15:43

    Your question is a bit unclear but I'm assuming you meant to ask the following

    When to I call the base constructor for my XNA object vs. using the impilict default constructor

    The answer to this is highly dependent on both your scenario and the underlying object. Could you clarify a bit wit the following

    • What is the scenario
    • What is the type of the base object of TerrainCollision?

    My best answer though is that in the case where you have parameters that line up with the parameters of the base class`s constructor, you should almost certainly be calling it.

    0 讨论(0)
  • 2020-11-22 15:44

    It will call the base constructor first. Also keep in mind that if you don't put the :base(param1) after your constructor, the base's empty constructor will be called.

    0 讨论(0)
  • 2020-11-22 15:45

    [Edit: in the time it took me to answer, the question had totally changed].

    The answer is that it calls the base first.

    [Original answer to the old question below]

    Are you asking when you would do the "base" bit of the constructor call?

    If so, you would "chain" a call to the constructor base if the class is derived from another class which has this constructor:

      public class CollisionBase
        {
            public CollisionBase(Body body, GameObject entity)
            {
    
            }
        }
    
        public class TerrainCollision : CollisionBase
        {
            public TerrainCollision(Body body, GameObject entity)
                : base(body, entity)
            {
    
            }
        }
    

    In this example, TerrainCollision derives from CollisionBase. By chaining the constructors in this way, it ensures the specified constructor is called on the base class with the supplied parameters, rather than the default constructor (if there is one on the base)

    0 讨论(0)
  • 2020-11-22 15:45

    Constructor mechanism is much better as it leaves the application to use constructor chaining and if you were to extend the application it enables through inheritance the ability to make minimal code changes. Jon Skeets Article

    0 讨论(0)
提交回复
热议问题