.Net中的ToString()方法 [英] ToString() method in .Net

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

问题描述

.ToString()
Convert.ToString()
众所周知,第一种情况将引发null异常.虽然秒会正常执行,但最佳实践始终是使用第二种情况.
为什么我们完全有第一种情况?什么时候可以用第二种情况做所有事情?
为什么.toString()是在.net中设计的?

请帮帮我.

.ToString()
Convert.ToString()
We all knows first case will throw a null exception. While second will execute normaly.And its best practice is always to use second case.
Why we have first case at all? when we can do everything with second case?
Why .ToString() is designed in .net?

Please help me out.

推荐答案

.ToString()如果要调用的对象为Null,肯定会抛出null异常吗?如果您是在空对象上调用它,则可能会在堆栈中进一步出错.

同样,许多类型或自定义类可以覆盖ToString以实现自定义实现,因此,像许多事情一样,由程序员决定如何最好地利用它们的可用范围.

http://msdn.microsoft.com/en-us/library/system.object. tostring.aspx [^ ]
.ToString() surely will throw a null exception if the object you are calling it on is Null? chances are if you are calling it on a null object, then something else has gone wrong further up the stack.

Also many types or custom classes can override the ToString for custom implementations, so like many things, its up to the programmer to decide how they best want to make use of what is available to them.

http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx[^]


ToString是.NET Framework中的主要格式化方法.它将对象转换为其字符串表示形式,以便适合显示.
当需要将数据类型更改为字符串时,应使用ToString.对于像int这样的内置类型,您期望的是字符串表示形式,因此我们可以说ToString允许您对自己的对象使用.net内置的复杂格式.您可以像datetime类一样提供复杂的格式化程序,以灵活地使用自己的类型.

查看给定的MSDN链接 Object.ToString方法[ ^ ]
Convert.ToString方法[
ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display.
ToString is should be used when you have a need to change a data type to a string. For built in types like int and such there string representations are what you expect hence we can say that ToString allows you to use the complex formating build into .net with your own objects. you can provide complex formatters like the datetime class does to give flexibility in using your own types.

Take a look at given MSDN Links Object.ToString Method[^]
Convert.ToString Method[^]
for more details


为什么Convert类是在.NET中设计的?

我们都知道Convert.ToString(null)会引发null异常.如果实例化了myObject,为什么使用此方法myObject.ToString()总是会返回正确的结果.为什么要完全采用第一种形式? :-)

认真地说,ToString是所有对象的基本方法.从根本上讲,它很重要,因为在需要字符串表示的任何地方,.NET库内部都会使用它.此方法是面向对象的,适用于多态容器.

相反,类Convert没有根本的重要性.它主要是实用程序方法的集合,主要用于方便开发人员.我注意到该类主要由经验不足的开发人员使用.但是,并非全部.一些方法确实很重要,例如方法ChangeType和base64方法的组.通常,这只是原始方法的集合,以方便使用.

—SA
Why Convert class is designed in .NET?

We all know that Convert.ToString(null) will throw a null exception. Why having this method is myObject.ToString() will always return a correct result if myObject was instantiated. Why having the first form at all? :-)

Seriously, ToString is a fundamental method of all objects. It is fundamentally important as it is used inside the .NET libraries internally everywhere a string representation is needed. This method is object-oriented, it is applied on polymorphous containers.

In contrast, the class Convert does not have fundamental importance. It''s mainly a collection of utility methods mostly used for convenience of the developers. I notices this class is mostly used by very inexperienced developers. Not all of it, though. Some methods are really important, such as group of the method ChangeType and base64 methods. In general, this is just a collection of primitive methods put together for convenience.

—SA


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

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