Manish Pansiniya's Blog

.NET, C#, Javascript, ASP.NET and lots more…:)

Difference Between ToString() vs Convert.ToString() vs (string) cast

with 25 comments

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.



Written by Manish

October 8, 2007 at 1:16 pm

Posted in .NET, .NET 3.0

25 Responses

Subscribe to comments with RSS.

  1. Nice


    November 30, 2007 at 7:04 am

  2. Good information.


    May 20, 2008 at 7:03 am

  3. It is Very Good

    Hukam Chand

    May 30, 2008 at 10:46 am

  4. There is no difference between the 2. Convert.ToString internally uses value.ToString


    August 21, 2008 at 7:02 am

  5. good


    September 17, 2008 at 3:54 am

  6. good issue,


    January 26, 2009 at 10:51 am

  7. Thanks…really nice to know..


    June 14, 2009 at 5:44 pm

  8. To anyone who says there is no difference. Yes there is:
    string s;
    object o = null;
    s = o.ToString();
    //returns a null reference exception for s.

    string s;
    object o = null;
    s = Convert.ToString(o);
    //returns an empty string for s and does not throw an exception. If you dont believe it, try it!

    Neither way is right or wrong. Use the approach that applies to what behavior you need for null objects.

    mr johnson

    October 30, 2009 at 6:54 am

    • string s = reader[“micclass”].ToString ();

      this value of miceclass come from data base it give null value but it convert in string why i could not understand ……………..


      January 27, 2010 at 6:04 pm

    • good interpretation mr johnson


      September 2, 2011 at 4:13 pm

  9. actually if reader[“micclass”] return null then this statement must throw an exception. I believe this (reader[“micclass”]) might return empty string.


    January 27, 2010 at 6:59 pm

    • reader[“micclass”]) will return DBNull object


      December 15, 2010 at 4:20 pm

  10. Thank you.
    This is was a question in an interview yesterday but.

    ahmed ali

    January 16, 2011 at 2:25 pm

  11. which should be used Covert.ToString() or (string) casting? which is the better way?


    June 19, 2011 at 5:44 pm

  12. It depends on your requrement.

    But if you use convert.tostring(), compare your return string with blank value string.empty when in needs. But when you cast it using (string), you should compare it with null too.

    You can use String.IsNullOrEmpty() function. It is the best function to check whether string is empty.


    June 20, 2011 at 3:09 am

  13. nice different

    Md nayeem

    August 9, 2011 at 4:25 pm

  14. […] Manish Pansiniya’s Blog PHP Freelancer .NET, Boas Práticas, Tips and tricksCast ← Padrões de […]

  15. Thanks for the answer

    pradeep kumar

    November 18, 2011 at 5:05 pm

  16. thanks johnson

    sam Eer

    January 11, 2012 at 11:46 am

  17. Good with thanks


    January 12, 2012 at 12:09 pm

  18. i want more clarity i did not understand plz give with more clarificationj


    February 3, 2012 at 12:18 pm

  19. Thanks for providing good Solution.

    umesh sharma

    February 12, 2012 at 11:49 pm

  20. Thanks ,
    I have got what i was looking for.

    Umesh Sharma

    February 13, 2012 at 12:01 am

  21. […] Referêcias: Manish Pansiniya’s Blog […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: