Loading ...

Difference between obj.ToString() vs Convert.ToString(obj) vs (string)obj vs obj as string | CodeAsp.Net

Difference between obj.ToString() vs Convert.ToString(obj) vs (string)obj vs obj as string

 /5
0 (0votes)

In this blog i will explain the difference between obj.ToString() vs Convert.ToString(obj) vs (string)obj vs obj as string

 

obj.ToString()


.ToString() can be called from any object. It returns a string that represents the current Object. ToString() is a method of object, and it will always work on a non-null reference. For example:

 

object str = "test string";

string newTestString = str.ToString();


str.ToString() will return "test string" as string .But

 

object str = null;

string newTestString = str.ToString();

 


Will throw the exception:“Object reference not set to an instance of an object.”
The above exception came because str is null we can not perform any operation on any object that has null reference.

Convert.ToString(obj)

Convert.ToString(obj) means we are calling a method and passing obj as argument.Internally it will call ToString(object value) method and then ToString(object value, IFormatProvider provider) method is called.These are the code internals:


public static string ToString(object value)

{

return ToString(value, null);

}

 

public static string ToString(object value, IFormatProvider provider)

{

IConvertible convertible = value as IConvertible;

if (convertible != null)

{

return convertible.ToString(provider);

}

IFormattable formattable = value as IFormattable;

if (formattable != null)

{

return formattable.ToString(null, provider);

}

if (value != null)

{

return value.ToString();

}

return string.Empty;

}



Now it is convertible which means:
http://msdn.microsoft.com/en-us/library/system.iconvertible.aspx
Defines methods that convert the value of the implementing reference or value type to a common language runtime type that has an equivalent value.

So convertible.ToString(provider); will return CLR string type object.

And it is formattable which means:
http://msdn.microsoft.com/en-us/library/system.iformattable.aspx
Provides functionality to format the value of an object into a string representation
So formattable.ToString(null, provider); will return CLR string type object.

If no condition matches string.Empty will be return this situation will come when the object that we passed is null for example:

Convert.ToString(null);

Converts the specified value to its equivalent String representation. Will return String.Empty if specified value is null.

 

(string)obj
First it should be clear that it is a cast operation, not a function (or method) call. We should use this if we are sure the object we are passing is of type string or it has an implicit or explicit operator that can convert the object to a string. This will return null if the object itself is null . See examples.

 

object str = "test string";

string newTestString = (string)str;

 

(string)str will return "test string" as string.But you cant do tht for Int.See below example:

object i = 2;

string str = (string)i;

 


The above code will throw this error:
Unable to cast object of type 'System.Int32' to type 'System.String'.Cannot cast expression of type int to string
Int doesn't have an explicit operator to string.When casting a number to a string, there is a "loss" in data. A string cannot perform mathematical operations, and it can't do number related things.

obj as string
It is safe cast operation. It is similar like (string)obj but instead of throwing an exception it will return null if cast operation fails. For example:

 

 

object i = 2;

string str = i as string;


str will return null.


So above we discuss the various cast and convert methods applied on object.
Do let me know your feedback,comments.

Comments (4)

   
tvance
very helpful thank you!  was trying to use the null coalescing operator with .ToString() and kept getting the object not set to an instance of an object error...very frustrating...thanks!
10/7/2009
 · 
by
   
rohit
thanks for the above information........
10/9/2009
 · 
by
   
Najeeb
Najeeb
oh. thanks Raghave. It s a great article. Its looks simple but said all the details. Thanks again.
10/22/2009
 · 
by
   
rtpHarry
Excellent explanation Raghav. As I have said before you have a brilliant grasp of the technicalities of the c# language and I am regularly impressed.
2/10/2010
 · 
by

Top Posts