C# Inheritance problem

I am seeing an issue where the child class’s version of a overridden function is not being called

public class A
{
}

public class B : A
{
public string GetText() { return “B Text”; }
}

public class C : B
{
new public string GetText() { return “C Text”; }
}

When i have an instance of C but refered to with an object reference of type B it is returning “B Text”

Is the “new” keyword not supposed to allow polymorphism?

“Feature works as defined.”

The MSDN has full information on how inheritance hierarchies work.

You are confusing the use of virtual/override (overriding the function in the parent class) and new (hiding the one in the parent class). Any object cast to class B will have no knowledge of the over-ridden functions in class C if the function is not already declared in a class further up the hierarchy without the use of the virtual keyword.

Aha thanks. I am used to C++. Didnt even think to use the same syntax. :wink: