Object.ToString()和Convert.ToString()之间的区别 [英] Difference between Object.ToString() and Convert.ToString()

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

问题描述

谁能告诉我,Object.ToString()和Convert.ToString()有什么区别?


请不要在此处放置链接....
在此先感谢

Can anyone tell me, What is the Difference between Object.ToString() and Convert.ToString()?


please don''t put links over here....
Thanks in advance

推荐答案

^ ]

好吧,我看到很多人说object.ToString()不处理Null值,我会说,如果它为null,它什么也不能处理.即

http://social.msdn.microsoft.com/forums/en-US/architecturegeneral/thread/bf118337-6dbd-4edd-9147-fbbcba7ce6c5/[^]

Well i saw lot of people saying object.ToString() doesn''t handle Null values, i''ll say if it''s null it cannot handle anything. i.e.

object a = null;
a.AnyFunction(); 


这将引发异常,因为对象引用未设置为与ToString()方法无关的对象实例.是CLR会引发异常,因为该对象为null

另一方面,Convert是一个帮助器类,它具有函数ToString和函数体是这样的:
已更新:@parmar_punit的评论之后.


this will raise an exception because object reference is not set to an instance of an object it has nothing to do with ToString() method. it''s the CLR who will raise the exception because the object is null

on the other hand Convert is a helper class which has a function ToString and the function body is something like this:
updated: after @parmar_punit''s comment.

public static string ToString(object obj)
{
    if(obj !=null)
    {
        return obj.ToString();
    }
return String.Empty;
}




如果object.Tostring()中的对象是值类型,则还有另外一件事,因为值成员如果没有声明为null,则在声明时会初始化内存,因为它们没有区别.

谢谢,
Hemant




one more thing if the object in object.Tostring() is value type there will be no difference becasue value member got there memory intitialized at time of declaration if they are not declared as null.

Thanks,
Hemant


基本区别在于,

Object.ToString()不能处理Null值,而Convert.ToString()可以处理Null值.这意味着Convert.ToString()不会生成Null引用异常,而Object.ToString()会生成Null引用异常.对NULL的对象执行.ToString().

例子:

The Basic Difference is that,

Object.ToString() not Handle Null Values where as Convert.ToString() can handle Null Values.That means Convert.Tostring() will not generate Null Refernce Exception where as Object.ToString() will generate Null Refernce Exception if you are trying to do .ToString() on object which is NULL.

Example :

nt x =0;
string s=x.ToString();
string Str=Convert.ToString(x);





Here We can convert the integer "x" using "x.ToString()" or "Convert.ToString()" . But only The basic difference between them is "Convert" function handles NULLS while "x.ToString()"
does not it will throw a NULL reference exception error. So as for good programming practice using "Convert" is always safe.



有关更多信息,请参见下面的链接:

[^ ]



For More Information refer below link:

[^]


如您所说的没有链接.但是,您可以简单地让我用Google [ ^ ]

这第一个ans与下面给出的ans .....或n个Ans ..



这三个之间有一个简单但重要的区别……

当对象为null时,ToString()引发异常

因此,在object.ToString()的情况下,如果object为null,则会引发NullReferenceException.

如果对象为null,则Convert.ToString()返回string.Empty

(字符串)强制转换为null的对象

所以在
的情况下 MyObject o =(string)NullObject;

但是,当您使用o访问任何属性时,它将引发NullReferenceException.
As You said No Links. But instead of Putting this Question here You Can Simply Do Let Me google [^]

This first ans would be same as ans given below..... Or n Number Of ans..



There is a simple but important difference between these three…

ToString() raise exception when the object is null

So in the case of object.ToString(), if object is null, it raise NullReferenceException.

Convert.ToString() return string.Empty in case of null object

(string) cast assign the object in case of null

So in case of
MyObject o = (string)NullObject;

But when you use o to access any property, it will raise NullReferenceException.


这篇关于Object.ToString()和Convert.ToString()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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