question

aviv-1548 avatar image
0 Votes"
aviv-1548 asked TimonYang-MSFT answered

In what order do things happen?

 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

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The code has an error and does not build or run. Once the error is fixed, Q is changed to q, then you can set a breakpoint and single step through the code. The order will be very clear.

0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered

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");
     }
 }




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.