A
A
AbnormalUnit2011-09-21 20:08:31
C++ / C#
AbnormalUnit, 2011-09-21 20:08:31

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

3 answer(s)
V
VenomBlood, 2011-09-21
@VenomBlood

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.

A
AbnormalUnit, 2011-09-21
@AbnormalUnit

Thank you all, got it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question