为什么String.ToString()? [英] Why String.ToString()?

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

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/2778827/c-why-does-the-string-type-have-a-tostring-method">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.

这篇关于为什么String.ToString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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