简体   繁体   English

显式与隐式调用ToString()

[英]Explicit vs implicit call of ToString()

Is there any benefit in calling the ToString() method on a string builder when the method call accepts the type object as it's parameter? 当方法调用接受类型对象作为参数时,在字符串生成器上调用ToString()方法有什么好处?

StringBuilder sb = new StringBuilder("Hello");

System.Console.WriteLine(sb);

OR 要么

System.Console.WriteLine(sb.ToString());

From what I know the WriteLine takes an object as an overload, my guess is the ToString() method is then called on the object, which then calls the StringBuilder.ToString() method and returns the expected string value. 据我所知,WriteLine将对象作为重载,我的猜测是然后在该对象上调用ToString()方法,然后该对象再调用StringBuilder.ToString()方法并返回预期的字符串值。

So again, is there any benefit in explictly calling ToString()? 再次重申,显式调用ToString()有什么好处吗? Could you please justify your answer? 你能证明你的答案合理吗?

The only benefit is that it is clear what you're doing. 唯一的好处是很清楚您在做什么。 If you can state something explicitly do it - it makes the code more readable. 如果您可以明确声明某些内容,则可以使代码更具可读性。

"my guess is the ToString() method is then called on the object" “我的猜测是然后在对象上调用ToString()方法”

If you look at MSDN you see that there is no overload that consumes a StringBuilder directly, so the one which accepts anything( object ) is used. 如果查看MSDN,您会发现没有直接使用StringBuilder重载,因此使用了接受any( object )的重载。

So your guess was correct, it will just call ToString . 因此,您的猜测是正确的,它将仅调用ToString

If value is null, only the line terminator is written. 如果value为null,则仅写入行终止符。 Otherwise, the ToString method of value is called to produce its string representation, and the resulting string is written to the standard output stream. 否则,将调用value的ToString方法以生成其字符串表示形式,并将结果字符串写入标准输出流。

Then i would prefer to call it myself since it's more readable and you gain nothing by passing the StringBuilder . 然后,我宁愿自己调用它,因为它更易读,并且通过传递StringBuilder一无所获。

Is there any benefit in explicitly calling ToString()? 显式调用ToString()有什么好处?

Console.WriteLine(object) will first check if the object is null and if so simply output an empty line. Console.WriteLine(object)将首先检查对象是否为null,如果是,则仅输出一个空行。 Calling ToString on a null object will result in a NullReferenceException. 在空对象上调用ToString将导致NullReferenceException。

So WriteLine(object) has built in null-safety, and the result for null will be the same as a non-null object that outputs an empty string. 因此WriteLine(object)已内置了null安全性,并且null的结果将与输出空字符串的非null对象相同。 Depending on the context, that null safety could be convenient, or it could cause undesired behavior. 根据上下文的不同,无效安全性可能很方便,或者可能导致不良行为。

In short, it's more beneficial to call ToString() if you don't want a NullReferenceException to be suppressed. 总之,它更利于调用toString(),如果你不想被抑制一个NullReferenceException

No, there's no benefit. 不,没有好处。 Tools with good static analysis capabilities like ReSharper will actually let you know that the call is redundant and can be safely removed. 实际上,具有良好静态分析功能的工具(例如ReSharper)将使您知道该调用是多余的并且可以安全地删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM