0

I have an example of code in C#

using System;
class A
{
  public virtual void F() { Console.WriteLine("A.F"); }
}
class B: A
{
  public override void F() { Console.WriteLine("B.F"); }
}
class C: B
{
  new public virtual void F() { Console.WriteLine("C.F"); }
}
class D: C
{
   public override void F() { Console.WriteLine("D.F"); }
}
class Test
{
static void Main() {
  D d = new D();
  A a = d;
  B b = d;
  C c = d;
  a.F();
  b.F();
  c.F();
  d.F();
}
}

This code outline

B.F
B.F
D.F
D.F

I do not understand why in case a.F(); There is B.F ?

how MS said the virtual methods is represended by type of object in real time

Roman
  • 371
  • 2
  • 3
  • 15

3 Answers3

1

I do not understand why in case a.F(); There is B.F ?

MSDN #

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

Bold content will explain to you why B.F is printed when A.F() is called.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
1

The call to method F() from your inherited class object prints B.F for the object of type D downcasted to type A, because you override the method in class B (and class D).

Your test is an almost exact copy of the example provided in this MSDN post on virtual methods:

using System;
class A
{
   public void F() { Console.WriteLine("A.F"); }
   public virtual void G() { Console.WriteLine("A.G"); }
}
class B: A
{
   new public void F() { Console.WriteLine("B.F"); }
   public override void G() { Console.WriteLine("B.G"); }
}
class Test
{
   static void Main() {
      B b = new B();
      A a = b;
      a.F();
      b.F();
      a.G();
      b.G();
   }
}

In the example, A introduces a non-virtual method F and a virtual method G. The class B introduces a new non-virtual method F, thus hiding the inherited F, and also overrides the inherited method G. The example produces the output:

A.F
B.F
B.G
B.G

Notice that the statement a.G() invokes B.G, not A.G. This is because the run-time type of the instance (which is B), not the compile-time type of the instance (which is A), determines the actual method implementation to invoke. Because methods are allowed to hide inherited methods, it is possible for a class to contain several virtual methods with the same signature. This does not present an ambiguity problem, since all but the most derived method are hidden. In the example

Wicher Visser
  • 1,513
  • 12
  • 21
1

In case of a.F() there is B.F because virtual void F in class A is override in B.

It is not override by C, because there is a new operator which does not override the virtual base method.

So if there is an instance of type D typed to A and you call F(), virtual method from the class A is called, but the latest override is taken into account. That is B.F.

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148