Convert.Tostring()和.ToString()的目的 [英] Purpose of Convert.Tostring() and .ToString()

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

问题描述

嗨朋友


我正在使用winform应用程序,正在使用Conver.ToSring()和.ToString()时转换概念.请您解释一下.


感谢U.

hi friends


i am using winform application,in converting concepts when i am using Conver.ToSring() and .ToString().can you please explain me.


Thanking U.

推荐答案

Convert.ToString()将处理null异常,并使用.ToString()将不会处理null异常.

在下面找到,
Convert.ToString() it will handle the null exceptions and using .ToString() It won''t handle the null exceptions.

find below,
Object obj = null;
String objValue1 = obj.ToString(); //throws Null reference exception
String objValue2 = Convert.ToString(obj);  //returns NULL



:)



:)


Sheker,

主要区别是每个类的基类都是Object类,而Object类的方法很少,ToString()是可以编写 overload override 的方法之一.
因此,当您调用object.ToString()时,它的意思是它将调用该对象ToString
如果object为null,则会抛出异常Object is not set to an instance of an object.

与在Convert.ToString方法中一样,Convert是一个静态类,我们在其中传递对象,并在该方法中放出其检查对象是否为null或否(如果不是,则调用该对象的ToString方法,否则返回null). />
这是主要区别现在您可以理解为什么这种行为差异

Hi Sheker,

Main difference is each class base class is Object class, and object class has few methods and ToString() is one of them you can write overload and override .

So when you call object.ToString() its mean it will call that objects ToString
there for if object is null then it will throw an exception Object is not set to an instance of an object.

Where as In Convert.ToString method, Convert is a static class where we pass our object and get the out put in that method its check where the object is null or no if not the call that objects ToString method other wise return null.

This is the main difference Now you could understand why this behavioral difference

object objec = null;
string objValue1 = objec .ToString(); //Exception
string objValue2 = Convert.ToString(objec); //return Null


此链接具有有很好的解释的答案:

http://social.msdn.microsoft.com /Forums/zh-CN/csharpgeneral/thread/ca084595-8502-4efe-b4be-d52ccb7d63c4/ [
this link has answer with very good explanation:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ca084595-8502-4efe-b4be-d52ccb7d63c4/[^]


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

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