为什么String.ToString()?String、ToString

2023-09-06 04:50:47 作者:玛丽莲萌鹿⌒_⌒

可能重复:   C#:为什么字符串类型具有的ToString()方法

为什么会出现的ToString 方法存在于字符串类(VB.NET)?

  String.ToString()
 

会不会是一个开销如果使用像

  TextBox.Text.ToString()
 

解决方案

的ToString 方法是在对象中找到字符串继承。实施 Object.ToString 是打印的类型名称。

 公共虚拟字符串的ToString(){
    返回this.GetType()的ToString()。
}
 
C 数值显示时,如何将十六进制F,显示为0F 通过toString X 怎么实现

类型字符串覆盖此方法返回本身。

 公共重写字符串的ToString(){
    回到这一点;
}
 

在code TextBox.Text.ToString()的ToString ,但它不必要的电话是不太可能会有这样做任何noticable性能影响。

Possible Duplicate: C#: why does the string type have a .ToString() method

Why is there a ToString method exist in String class (VB.NET)?

String.ToString()

Will it be a overhead if it is used like

TextBox.Text.ToString()

解决方案

The ToString method is found on Object from which String inherits. The implementation of Object.ToString is to print the typename.

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

The type String overrides this method to return itself.

public override string ToString() {
    return this;
} 

The code TextBox.Text.ToString() has an unnecessary call to ToString, but it is unlikely that there will be any noticable performance impact from doing so.