可能重复: C#:为什么字符串类型具有的ToString()方法
为什么会出现的ToString
方法存在于字符串
类(VB.NET)?
String.ToString()
会不会是一个开销如果使用像
TextBox.Text.ToString()
解决方案
在的ToString
方法是在对象中找到
从字符串
继承。实施 Object.ToString
是打印的类型名称。
公共虚拟字符串的ToString(){
返回this.GetType()的ToString()。
}
类型字符串
覆盖此方法返回本身。
公共重写字符串的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.
下一篇:如何合并数据集中的两列?数据