Because he wanted to do something?
The system calls your static Main method when it starts your application: from that point on it's up to you to tell it exactly what to do. If you don't explicitly call a method (or set an handler for an event) then the code you have written will not be executed.
In this specific case, the code constructs an instance of the
Demo1
class, and calls the
a
method via the
Data
reference called
obj1
. The system looks at the reference, finds no overloads as calls the base class method.
You might have noticed that when you compiled this you got a warning: these aren't as serious as errors in that they won't stop you application from running, but (particularly when you are just learning) it's a very good idea to change your settings so the do (Easy enough, just double click your projects "Properties" branch in the Solution explorer pane and change the "Build" tab option "Treat warnings as errors" to "All").
Because the original base class method isn't marked as virtual, the system doesn't even try to look for a "latest version" to call, so unless the reference is to a
Demo1
instance, it will always call the "lower" version. Try like this instead:
public class Data
{
public virtual void a()
{
Console.WriteLine("Base class Method");
}
}
public class Demo1 : Data
{
public override void a()
{
Console.WriteLine("Child class Method");
}
public string Result = "a";
}