java converttostring_Convert.ToString()和.ToString()之间的区别

Convert.ToString()处理null ,而ToString()不处理。

在对象上调用ToString()假定对象不是null(因为对象需要存在才能调用实例方法)。 Convert.ToString(obj)不需要假定对象不是null(因为它是Convert类的一个静态方法),但是如果它是 null,将会返回String.Empty 。

让我们通过这个例子来了解不同之处:

int i= 0; MessageBox.Show(i.ToString()); MessageBox.Show(Convert.ToString(i));

我们可以使用i.ToString ()或Convert.ToString来转换整数。 那有什么区别?

他们之间的基本区别是Convert函数处理i.ToString ()而i.ToString ()不; 它会抛出一个NULL引用exception错误。 所以使用convert良好编码习惯总是安全的。

除了处理null值的其他答案之外, Convert.ToString在调用基类IFormattable之前尝试使用IFormattable和IConvertible接口。

例:

class FormattableType : IFormattable { private double value = 0.42; public string ToString(string format, IFormatProvider formatProvider) { if (formatProvider == null) { // ... using some IOC-containers // ... or using CultureInfo.CurrentCulture / Thread.CurrentThread.CurrentCulture formatProvider = CultureInfo.InvariantCulture; } // ... doing things with format return value.ToString(formatProvider); } public override string ToString() { return value.ToString(); } }

结果:

Convert.ToString(new FormattableType()); // 0.42 new FormattableType().ToString(); // 0,42

您可以创build一个类并重写toString方法来执行任何您想要的操作。

例如,您可以创build一个“MyMail”类,并覆盖toString方法来发送电子邮件或执行其他操作,而不是写入当前对象。

Convert.toString将指定的值转换为其等效的string表示forms。

在Convert.ToString() ,Convert处理NULL值或不处理.ToString()它不处理NULL值和NULL引用exception错误。 所以最好使用Convert.ToString() 。

object o=null; string s; s=o.toString(); //returns a null reference exception for string s. string str=convert.tostring(o); //returns an empty string for string str and does not throw an exception.,it's //better to use convert.tostring() for good coding

对于爱好者来说,这是最好的答案。

.............. Un Safe code ................................... Try ' In this code we will get "Object reference not set to an instance of an object." exception Dim a As Object a = Nothing a.ToString() Catch ex As NullReferenceException Response.Write(ex.Message) End Try '............... it is a safe code.............................. Dim b As Object b = Nothing Convert.ToString(b)

除了处理null 之外 ,这些方法“基本上”是相同的。

Pen pen = null; Convert.ToString(pen); // No exception thrown pen.ToString(); // Throws NullReferenceException

来自MSDN:

Convert.ToString方法

将指定的值转换为其等效的string表示forms。

Object.ToString

返回表示当前对象的string。

ToString() Vs Convert.ToString()

相似之处: –

两者都用于将特定types转换为string,即将int转换为string,将其浮点到string或将对象转换为string。

区别 :-

在Convert.ToString()将处理空值的情况下ToString()不能处理null。

例如:

namespace Marcus { class Employee { public int Id { get; set; } public string Name { get; set; } } class Startup { public static void Main() { Employee e = new Employee(); e = null; string s = e.ToString(); // This will throw an null exception s = Convert.ToString(e); // This will throw null exception but it will be automatically handled by Convert.ToString() and exception will not be shown on command window. } } }

ToString()不能处理空值和convert.ToString()可以处理空值,所以当你想要你的系统处理空值使用convert.ToString() 。

Convert.ToString(strName)将处理空值, strName.Tostring()不会处理空值并抛出exception。

所以最好使用Convert.ToString()然后.ToString();

我同意@ Ryan的回答。 顺便说一下,从C#6.0开始,为此可以使用:

someString?.ToString() ?? string.Empty;

要么

$"{someString}"; // bad practice, but this is very concise

代替

Convert.ToString(someString);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值