I have a problem (it's my fault, I just can't spot what I'm doing wrong) where "ToString" isn't calling the correct method…
public class ClassA
{
public override ToString()
{
return "Hello, I'm class A.";
}
}
public class ClassB : ClassA
{
public override ToString()
{
return "Hello, I'm class B.";
}
}
ClassB myClassB = new ClassB();
List<ClassA> myClassAList = new List<ClassA>();
myClassAList.Add((ClassA) myClassB);
ClassA tempClassA = myClassAList[0];
Console.WriteLine(tempClassA.ToString());
I'm getting the "ToString" from "ClassB" and not "ClassA" what am I doing wrong?
Best Solution
You are overriding ToString in ClassB instead of hiding it from the original which will cause the overridden method to take precedence. What you could do is..