1.

What Is Difference Between Tostring() Vs Convert.tostring() Vs (string) Cast

Answer»

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.

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.



Discussion

No Comment Found