Marking ToString virtual in base class, what happens?

匿名 (未验证) 提交于 2019-12-03 00:58:01

问题:

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();   } } 

回答1:

That's creating a new virtual method in X called ToString() which hides Object.ToString(). So if you have:

Y y = new Y(); X x = y; Object o = y;  Console.WriteLine(y.ToString()); // Shows "Hi, I'm Y, Hi, I'm X"; Console.WriteLine(x.ToString()); // Shows "Hi, I'm Y, Hi, I'm X"; Console.WriteLine(o.ToString()); // Calls object.ToString; shows just "Y" 

Calling just

Console.WriteLine(y); 

is equivalent to the final line, which is why the type name is printed.

Basically, your X.ToString method should override the object.ToString() method:

public override String ToString()  {     return "Hi, I'm X"; } 


回答2:

By using virtual String ToString() on class X, you're "hiding" object.ToString instead of overriding it.

When you call Console.WriteLine(y);, it calls object.ToString(). Since you didn't override this, your method never gets called.

That being said, the compiler will warn you:

Warning 1 'X.ToString()' hides inherited member 'object.ToString()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!