Reputation: 37
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?
Upvotes: 0
Views: 135
Reputation: 57
You need to move your debugger's arrow (the yellow one, idk what's the name) until it past the
name = "22";
Upvotes: 0
Reputation: 151690
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:
class B:A
{
string name;
public B()
{
// hidden from debugger
name = "11"
// here's where the debugger is told the constructor starts
name = "22";
}
}
That's why your breakpoint on public B()
shows that name
is already initialized.
Upvotes: 4