42
object obj = "Hello";
string str1 = (string)obj;
string str2 = obj.ToString();

What is the difference between (string)obj and obj.ToString()?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135
  • 6
    As I mentioned in my comment to Guillaume, this has nothing to do with boxing. String is a reference type - there's no need for boxing. – Jon Skeet Oct 14 '09 at 09:09
  • 1
    Possible duplicate of [Casting vs Converting an object toString, when object really is a string](http://stackoverflow.com/questions/1170756/casting-vs-converting-an-object-tostring-when-object-really-is-a-string) – Jim Fell May 24 '16 at 17:35

5 Answers5

49
  • (string)obj casts obj into a string. obj must already be a string for this to succeed.
  • obj.ToString() gets a string representation of obj by calling the ToString() method. Which is obj itself when obj is a string. This (should) never throw(s) an exception (unless obj happens to be null, obviously).

So in your specific case, both are equivalent.

Note that string is a reference type (as opposed to a value type). As such, it inherits from object and no boxing ever occurs.

Mac
  • 8,191
  • 4
  • 40
  • 51
  • 1
    One thing to note is that obj, str1 and str2 all reference the same object. So Explicitly casting an object that is a string into a string, will return the objectreference itself. – Yannick Motton Oct 14 '09 at 09:18
  • 9
    An object doesn't necessarily have to be a string for it to succeed. It also works if there is an explicit conversion operator defined. – Yannick Motton Oct 14 '09 at 09:24
  • 1
    You are obviously right about the conversion operator. But for the sake of non confusion, I'll keep it in the comments. Thanks for pointing this out. – Mac Oct 14 '09 at 09:26
  • 5
    if obj is null then ToString() will throw a exception. – Rodolfo Nov 24 '16 at 15:35
  • 1
    Rodolfo is correct - if `obj` is null (even if it is a null string) then `obj.ToString()` will throw an exception. Instead, you can use `obj?.ToString()` which will produce the same effect and not throw an exception if `obj` is null – derekantrican Aug 18 '20 at 16:59
18

If its any help, you could use the 'as' operator which is similar to the cast but returns null instead of an exception on any conversion failure.

string str3 = obj as string;
Nick Masao
  • 1,088
  • 12
  • 25
9

At the most basic level:

(string)obj will attempt to cast obj to a string and will fail if there's no valid conversion.

obj.ToString() will return a string that the designer of obj has decided represents that object. By default it returns the class name of obj.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
2

(string)obj cast the object and will fail if obj is not null and not a string.

obj.ToString() converts obj to a string (even if it is not a string), it will fail is obj is null as it's a method call.

Guillaume
  • 12,824
  • 3
  • 40
  • 48
1

ToString() is object class method (the main parent class in .net) which can be overloaded in your class which inherits from object class even if you didn't inherited from it.

(string) is casting which can be implemented in the class it self, the string class so you don't have ability on it.

amr osama
  • 1,129
  • 2
  • 18
  • 34