In IL code,the field initialization is in constructor.
Field initialization in Constructor
But in VS2017 debug ,the field initialization is not in constructor, but in class.
Field initialization in VS Debug
Source Code:
class A
{
public int id = 0;
public A()
{
id = 99;
}
}
class B:A
{
string name = "11";
public B()
{
name = "22";
}
}
class Program
{
static void Main(string[] args)
{
B b = new B();
}
}
Can someone explain this to me?
That doesn't look like a problem. The compiler moves field initializations into the constructor, but the debug information tries to follow the C# code as closely as possible.
So actually your IL will look something like this:
That's why your breakpoint on
public B()
shows thatname
is already initialized.