C#:为什么字符串类型都有的ToString()方法 [英] C#: why does the string type have a .ToString() method

查看:109
本文介绍了C#:为什么字符串类型都有的ToString()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#:为什么字符串数据类型有一个的ToString()

C#: Why does the string data type have a .ToString() method?

推荐答案

该型 System.String ,如几乎在.NET中的所有类型,从 System.Object的 对象 的ToString() 法等字符串继承这个方法。这是一个虚拟的方法和字符串覆盖它的引用返回本身,而不是使用默认的实现,它是返回类型的名称。

The type System.String, like almost all types in .NET, derives from System.Object. Object has a ToString() method and so String inherits this method. It is a virtual method and String overrides it to return a reference to itself rather than using the default implementation which is to return the name of the type.

从反射镜,这是的ToString在对象实施

From Reflector, this is the implementation of ToString in Object:

public virtual string ToString()
{
    return this.GetType().ToString();
}

和这是字符串

public override string ToString()
{
    return this;
}

这篇关于C#:为什么字符串类型都有的ToString()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆