Answer the question
In order to leave comments, you need to log in
C# inheritance
There is a code:
Well, why does it output:
A
B
B
Why does it output an overloaded class method, the base class for C?
Thank you!
using System;
namespace SomeStuff
{
class MainClass
{
public static void Main (string[] args)
{
A a = new A();
B b = new B();
A c = new C();
a.Display();
b.Display();
c.Display();
}
public class A
{
public virtual void Display()
{
Console.WriteLine("A");
}
}
public class B : A
{
public override void Display ()
{
Console.WriteLine("B");
}
}
public class C : B
{
public new void Display()
{
Console.WriteLine("C");
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Everything is doing right. The type is specified as A, the method is virtual, the method hierarchy ends with B, a subtype of type B is created. When the method is called, overloads are checked along the inheritance hierarchy, where the most appropriate overload is in class B.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question