.NET: Redundant ToString() method

It is only when I used Resharper that I learned I have been redundantly calling ToString() method when concatenating other types with a string type and assign it into string variable. The code below work just fine despite different types involved in the expression.

    1         int value1 = 100;

    2         decimal value2 = 200.00M;

    3         float value3 = 300;

    4         object value4 = new object();

    5         string querystring = Request.QueryString["key"];

    6 

    7         //ok

    8         string result1 = querystring + value4 + value3 + value2 + value1;

    9         //ok

   10         string result2 = value3 + querystring + value2 + value1 + value4;

   11         //ok

   12         string result3 = value1 + value1 + value1 + "";


HTH

Share this post: Email it! | bookmark it! | digg it! | reddit!| kick it!
Filed under: ,

Comments

# cruizer said:

that's cool, didn't know it either. although i would probably still prefer to explicitly put .ToString() to convert the variables to string before concatenating them, just to clarify the intention.

Sunday, January 14, 2007 11:58 PM
# Jon Limjap said:

I for one would prefer to see the ToString()s being called explicitly. I don't have confidence in implicit conversions, plus I could easily put format modifiers between those empty parentheses.

Monday, January 15, 2007 8:40 AM
# keithrull said:

i think not putting the .ToString() is a little bit confusing...

Monday, January 15, 2007 5:54 PM
# jokiz said:

didn't know about it too, but i'd prefer using string.Format("{0}{1}{2}", value1, value2, value3) instead of concatenations

Monday, January 15, 2007 6:54 PM
# cruizer said:

yep jokiz that seems to be ReSharper's recommendation whenever you do lots of string concatenations also involving string constants

Tuesday, January 16, 2007 1:21 PM
# rdagumampan said:

i agree, when many variables are involved I also prefer string.Format.

Tuesday, January 16, 2007 1:26 PM