I think I have a miss concept. I am trying to override the behavior of a class in a subclass by replacing a public function. In the program below, I expect "B" to be written to the console, but the program will print "A". Where am I thinking wrong? And how can I achieve this. (In my real case, I cannot change class A).
class Program
{
class A { public void F() { Console.WriteLine("A"); } }
class B : A { public new void F() { Console.WriteLine("B"); } }
static void Main(string[] args)
{
A x;
x = new B();
x.F();
Console.ReadLine();
}
}
source
share