If you use the Step Into in the following program. Yellow line is jump into the statement Console.WriteLine(base.GetType().Name); within the two curly brackets without going through private void k().
What is the reason for that? Problem is highlighted.
using System;
namespace contest
{
class Program
{
static void Main(string[] args)
{
B b = new B();
A a = new A();
Console.Read();
}
}
class A
{
public A()
{
k();
}
private void k()
{
Console.WriteLine(base.GetType().Name);
}
}
class B : A { }
}
Loading
VulpesPosted Oct 6, 2014, 6:20 AM
This is documented behavior. See:
http://msdn.microsoft.com/en-us/library/y740d9d3.aspx
However, 'explicit' constructors seem to be an exception to this i.e. the debugger does stop on the constructor declaration even if the class doesn't contain any field initializers which would execute before the constructor does.
If the class does contain 'explicit' field initializers, then the debugger would stop on each of these in turn, as Wim indicated it would, and then stop on the constructor declaration.
'Implicit' constructors (i.e. those which are supplied by the system such as B's constructor in the example) are skipped altogether and so too are field declarations if they don't contain an 'explicit' initializer despite the fact that the field will be given its default value in such cases.
You can check that this is so by changing:
int anint = 0;
to just:
int anint;
in Win's code.
Posted Oct 6, 2014, 3:20 PM
Wim SturkenboomPosted Oct 5, 2014, 8:56 AM
For the fun, add an int to the class like below and see where F11 takes you.
class A
{
int anint = 0;
public A()
{
k();
}
private void k()
{
Console.WriteLine(base.GetType().Name);
}
}
It will stop at the declaration of anint.