gpt4 book ai didi

c# - 为什么字符串类型有.ToString() 方法?

转载 作者:IT王子 更新时间:2023-10-29 03:53:11 25 4
gpt4 key购买 nike

为什么字符串数据类型有.ToString()方法?

最佳答案

类型System.String , 比如 almost .NET 中的所有类型,派生自 System.Object . 对象 有一个 ToString()方法,所以 String 继承了这个方法。它是一个虚拟方法,String 重写它以返回对自身的引用,而不是使用返回类型名称的默认实现。

从Reflector,这是Object中ToString的实现:

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

这是 String 中的覆盖:

public override string ToString()
{
return this;
}

关于c# - 为什么字符串类型有.ToString() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2778827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com