I'm practicing the member initialize order when an object is created.
I know the order this:
- static variable 0 initialize
- static variable initialize syntax
- base class static constructor
- static constructor
- instance variable 0 initialize
- instance variable initialize syntax
- base class instance constructor
- instance constructor
Am I taught correctly?
And here is the code I tested:
public class Person
{
1 public static int PersonAge = Man.ManAge;
static Person()
{
2 Man.ManAge++;
3 Console.WriteLine($"Person : {PersonAge}");
}
}
public class Man : Person
{
4 public static int ManAge = 2;
static Man()
{
5 Console.WriteLine($"Man : {ManAge}");
}
}
var man = new Man();
I was expecting the following execution result:
Person : 3
Man : 3
But, the result is:
Man : 2
Person : 2
I think you have to 1 → 4 → 5 → 2 → 3 in order to proceed as the reuslt.
But, as I have learned, 4 → 2 → 1 → 3 → 5.
What am I doing wrong?