You need to understand the code below for C # virtual methods

this is a small code that shows virtual methods.

class A { public virtual void F() { Console.WriteLine("AF"); } } class B: A { public override void F() { Console.WriteLine("BF"); } } class C: B { new public virtual void F() { Console.WriteLine("CF"); } } class D: C { public override void F() { Console.WriteLine("DF"); } } class Test { static void Main() { D d = new D(); A a = d; B b = d; aF(); bF(); } } 

This code prints the bottom output:

 BF BF 

I cannot understand why aF () will print BF?

I thought he would print DF because class B overrides F () of class A, then this method is hidden in class C using the β€œnew” keyword, and then redefined in class D. So, finally, DF remains.

but this is not so. Could you explain why it prints BF?

+6
source share
4 answers
 A a = d; aF(); 

He will find F() as follows.

  • First in class A
  • Then in class B
  • Then in class C
  • Then in class D

Now F() will be found in A and B Thus, BF() will be called. In class C F () is different (since this is a new implementation and does not override A / B). So, in step 3, cF () will not be found. In class D, it overrides the new function created by C, so it will not be found either.

Due to the new keyword, the resulting code looks like this (regarding virtual override)

  class A { public virtual void F() { Console.WriteLine("AF"); } } class B: A { public override void F() { Console.WriteLine("BF"); } } class C: B { public virtual void F1() { Console.WriteLine("CF"); } } class D: C { public override void F1() { Console.WriteLine("DF"); } } 
+5
source

Unlike overriding a method that allows polymorphism, hiding the method using the new keyword is just a naming issue (and note that using new just removes the warning that you are hiding something).

In class C , when you declare:

 new public virtual void F() { ... } 

Here you define a completely new method that is not related to the superclass F() , which has the same name.

When an instance of F assigned to a variable of type A or B , a call to F() using these variables indicates the method defined by the superclass.

Imagine if you didn’t call the C F() method, but something like G() . The following code will not compile:

 aG(); bG(); 

Since with variables statically entered as A or B , the compiler cannot see the newly declared method. This is the same situation in your example, except that the superclass is used for the original method named F() .

+2
source

AF is overridden by BF Therefore, when AF is called, it will execute BFBF 'connected' to 'A.F'.

Now CF hides BF with a new keyword. CF is not connected to BF / AF. When you create C and F () is called, it will execute CF. If you press C on type A or B and call F, it will execute BF since BF and AF are connected

CF is redefined by DF They are related. When CF or D.FF is called, it will execute DF Now, when you throw object D at or B and call F (), it calls BF

Hope this helps make it clearer.

+1
source

As others have already pointed out, a new keyword in C hides the overload of F, so there will be no suitable overload of F.

If you need more details on C # where you will find the correct overload, read these two:

John Skeet: C # in depth, chapter Overload

Visual Studio 2010: Breaking Changes, here: Overload Resolution

0
source

Source: https://habr.com/ru/post/919817/


All Articles