Consider the following (LinqPad) example. ToString in class X is marked virtual. Why is the output here not equal to “Hi, I’m Y, Hi, I’m X” but instead the typename is printed? Of course marking ToString virtual is wrong, because it is defined in Object as virtual, I am just trying to understand what is happening here.
void Main()
{
Y y = new Y();
Console.WriteLine(y);
}
// Define other methods and classes here
class X
{
public virtual String ToString()
{
return "Hi, I'm X";
}
}
class Y : X
{
public override String ToString()
{
return "Hi, I'm Y, " + base.ToString();
}
}
That’s creating a new virtual method in
XcalledToString()which hidesObject.ToString(). So if you have:Calling just
is equivalent to the final line, which is why the type name is printed.
Basically, your
X.ToStringmethod should override theobject.ToString()method: