-1

I was hoping to see if there is an easy way to Replace ().ToString() with Convert.ToString() throughout the application using Code Resharper or CodeRush or any other tool ?

Thanks

Surya Garimella
  • 317
  • 4
  • 13
  • Keep in mind that Convert.ToString() is also going to be significantly slower and won't allow you to do formatting. – SledgeHammer Sep 22 '16 at 20:48
  • 3
    Are you fortunate enough to be using the latest and greatest compiler everywhere? You should be able to replace `.ToString()` with `?.ToString()`. You may need a regular expression or some creativity to ensure that you don't accidentally replace `?.ToString()` with `??.ToString()` – Joe Enos Sep 22 '16 at 20:49
  • One thing to note, if this is done for concatenation, you don't need `.ToString()` or `Convert.Tostring(...)`. This syntax: `var anything = (anything); string message = "The value is " + anything;` is valid for any type. `anything` can be a value type or class, and null is automatically converted to empty. – Mr Anderson Sep 22 '16 at 21:25

1 Answers1

6

object.ToString() does not equal Convert.ToString(). The former will thrown an exception if object is null, so making the changes you suggested would be a breaking change because the application may handle the exception, for example.

See this post: Difference between Convert.ToString() and .ToString()

Community
  • 1
  • 1
rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • Thank you. My purpose is to convert the type to a string by avoiding null exceptions. Is there any way i can achieve this conversion using any tools like Code Rush, Resharper, etc ? – Surya Garimella Sep 22 '16 at 20:43
  • @JoeEnos Thank you..! That helped. I forgot about the null confitional operator. – Surya Garimella Sep 23 '16 at 13:54
  • @SuryaGarimella -- I think you meant to put that comment under your question where Joe Enos posted his comment, not under my answer. – rory.ap Sep 23 '16 at 13:55