c# – .ToString()方法如何工作?

c# – .ToString()方法如何工作?,第1张

概述有时当我调用类的.ToString()方法时,它会返回类的完全限定名.但对于某些类的/ struct(如Int32),它返回一个字符串对应的对象(整数的值).这是否意味着Int32类重写了ToString()方法,并且返回完全限定名称的类不会覆盖它,而只是调用base(Object)的ToString()方法? Object.ToString()实现是否只返回类的完全限定名称? Sometimes 有时当我调用类的.ToString()方法时,它会返回类的完全限定名.但对于某些类的/ struct(如Int32),它返回一个字符串对应的对象(整数的值).这是否意味着Int32类重写了ToString()方法,并且返回完全限定名称的类不会覆盖它,而只是调用base(Object)的ToString()方法? Object.ToString()实现是否只返回类的完全限定名称?解决方法

Sometimes when I call the ToString method it returns the fully qualifIEd name of the runtime type of the object that received the call.

正确.

But for some types,such as system.int32,ToString returns the value of the receiver converted to a string.

正确.

Does the system.int32 struct overrIDe the ToString method?

是.

Do other types whose ToString methods return the fully-qualifIEd type name not overrIDe ToString?

情况可能就是这样,是的.当然,他们可以覆盖该方法并使覆盖方法与基类方法完全相同,但这有点无意义.

So in those cases,calling ToString just calls the System.Object implementation of ToString,which returns fully qualifIEd name?

正确.

你似乎已经掌握了它是如何工作的.我唯一的修正是要注意system.int32是一个结构,而不是一个类.

总结

以上是内存溢出为你收集整理的c# – .ToString()方法如何工作?全部内容,希望文章能够帮你解决c# – .ToString()方法如何工作?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1240948.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存