posted 9/26/2009 by Raghav Khunger
In this blog i will explain the difference between obj.ToString() vs Convert.ToString(obj) vs (string)obj vs obj as string
.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;
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)
IFormattable formattable = value as IFormattable;
if (formattable != null)
return formattable.ToString(null, provider);
if (value != null)
Now it is convertible which means:http://msdn.microsoft.com/en-us/library/system.iconvertible.aspxDefines 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.aspxProvides functionality to format the value of an object into a string representationSo 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)objFirst 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.
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 stringIt 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:
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.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?