Two classes, one overrides Object.ToString()
and one just hides it:
public class One
{
public override string ToString() => "One";
}
public class Two : One
{
public new string ToString() => "Two";
}
Then, I'm calling ToString
in two different ways:
var item = new Two();
Console.WriteLine(item.ToString()); // first way
Print(item); // second way
Where Print
is
public static void Print<T>(T item)
=> Console.WriteLine(item.ToString());
As result I get
Two
One
Which is not what I expected. My expected behavour would be that both calls will print the same text — Two
. I thought that compiler will understand correctly what type is passed and, according to the type, correct method will be called, as T
should be Two
.
Acquiring type name proves that it's actually Two
, but method resolution still works strangely.
Could you please explain what am I seeing? Is boxing involved somehow?