In what order do things happen?

aviv 1 Reputation point
2021-07-04T08:53:30.307+00:00
class A 
{
    int p = 8; 
}

class B : A
{
    int q = 9;

    public B() { Q++; }

}

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
    }
}

In this code what order do things happen?

1 - Initialize class B q field to default value (zero)
2 - Initialize class A p field to default value (zero)
3 - Placing the value 9 in class B q field
4 - Placing the value 8 in class A p field
5 - calling to class A constructor
6 - the command q++ in the class B constructor

In what order it works?
options:
(a) 1-5-2-4-3-6
(b) 1-3-5-2-4-6

Sorry for the bad English, hope the question can be understood.
Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,857 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,541 Reputation points
    2021-07-04T17:21:19.763+00:00

    See WhatOrder. You can see the result of the execution at the bottom of that. You can use Console.WriteLine statements like that to answer questions like this. The following is the code in the fiddle.

    public class Program
    {
        public static void Main()
        {
            B b = new B();
        }
    }
    
    public class A
    {
        int p = 8;
        public A()
        {
            Console.WriteLine("Constructor for A");
        }
    }
    
    public class B : A
    {
        int q = 9;
        public B()
        {
            q++;
            Console.WriteLine("Constructor for B");
        }
    }
    
    0 comments No comments

  2. Bruce (SqlWork.com) 64,566 Reputation points
    2021-07-04T18:34:16.187+00:00

    while the rules are different for static classes and fields, for instance classes, it is initializers from derived to base, then constructors from base to derived. so in you case, (with the q fix)

    1 - Initialize class B q field to default value (zero)
    2 - Initialize class A p field to default value (zero)
    3 - Placing the value 9 in class B q field
    4 - Placing the value 8 in class A p field
    5 - calling to class A constructor
    6 - the command q++ in the class B constructor

    1 and 2 never happen (default is set by initializer, and because a default is defined the specified value is used instead). so its

    (none of the above) 3,4,5,6

    0 comments No comments

  3. Timon Yang-MSFT 9,591 Reputation points
    2021-07-05T02:24:00.953+00:00

    The order of C# object initialization is:

    1. Derived static fields
    2. Derived static constructor
    3. Derived instance fields
    4. Base static fields
    5. Base static constructor
    6. Base instance fields
    7. Base instance constructor
    8. Derived instance constructor

    In this example, your question involves 3, 6, 7, 8, corresponding to your order of 3, 4, 5, 6.

    There is a more detailed code example in this link, to prevent this link from being unavailable one day in the future, I will extract it here.

        class Program
        {
            static void Main(string[] args)
            {
                Derived d = new Derived();
                Console.ReadLine();
            }
        }
        class Base
        {
            public Base()
            {
                Console.WriteLine("Base.Instance.Constructor");
                this.m_Field3 = new Tracker("Base.Instance.Field3");
                this.Virtual();
            }
    
            private Tracker m_Field1 = new Tracker("Base.Instance.Field1");
            private Tracker m_Field2 = new Tracker("Base.Instance.Field2");
            private Tracker m_Field3;
            static private Tracker s_Field1 = new Tracker("Base.Static.Field1");
            static private Tracker s_Field2 = new Tracker("Base.Static.Field2");
            virtual public void Virtual()
            {
                Console.WriteLine("Base.Instance.Virtual");
            }
        }
        class Derived : Base
        {
            public Derived()
            {
                Console.WriteLine("Derived.Instance.Constructor");
                this.m_Field3 = new Tracker("Derived.Instance.Field3");
            }
    
            private Tracker m_Field1 = new Tracker("Derived.Instance.Field1");
            private Tracker m_Field2 = new Tracker("Derived.Instance.Field2");
            private Tracker m_Field3;
            static private Tracker s_Field1 = new Tracker("Derived.Static.Field1");
            static private Tracker s_Field2 = new Tracker("Derived.Static.Field2");
            override public void Virtual()
            {
                Console.WriteLine("Derived.Instance.Virtual");
            }
        }
        class Tracker
        {
            public Tracker(string text)
            {
                Console.WriteLine(text);
            }
        }
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.