2

Which of these will achieve the correct result:

(1)

int X = 23;
string str = "HELLO" + X.ToString() + "WORLD";

(2)

int X = 23;
string str = "HELLO" + X + "WORLD";

(3)

int X = 23;
string str = "HELLO" + (string)X + "WORLD";

EDIT: The 'correct' result is for str to evaluate to: HELLO23WORLD

CJ7
  • 22,579
  • 65
  • 193
  • 321
  • I have to say it, What is the correct result? – LizB May 06 '10 at 06:23
  • Well, except for the fact that there's no such thing as `toString()` in C#, it's `ToString()`. – Dean Harding May 06 '10 at 06:23
  • Try it and see. You've got a compiler handy, don't you? You'd quickly recognize a typo in the first version if you did. In any event, Darin's answer is better. – JasonTrue May 06 '10 at 06:23
  • 1
    You realize 1 won't compile (type, ToString() needs to be capitalized) and 3 will give you an invalid cast exception, right? – Roman May 06 '10 at 06:24
  • @R0MANARMY: OK, so you're saying the answer can't be 3. – CJ7 May 06 '10 at 06:36
  • @Craig Johnston: I ran 3 in Linqpad as a sanity check and got an error, so yes, I'm pretty sure it can't be 3. – Roman May 06 '10 at 06:47

3 Answers3

7
int X = 23;
string str = string.Format("HELLO{0}WORLD", X);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Please leave a comment when down-voting. Is there something wrong with my answer? – Darin Dimitrov May 06 '10 at 06:47
  • -1: the question asked which of the three code segments produced a correct result, but your answer is still useful because it shows an alternative method, but it is not answering the question – CJ7 May 06 '10 at 06:52
  • +1: **much** better than concatenating strings. It feels wrong that the answerer got penalized simply because he did not explain why it is a better option than the 3 posted in the question. – ANeves May 18 '10 at 16:11
  • The question asked which of the three was correct. Creating a fourth option is not a valid answer to the question. – CJ7 May 19 '10 at 04:44
4

Option 3 doesn't compile cause you cannot cast an int to string.

The two others produce the same result. However, there's a subtle difference.

Internally the plus operator compiles to a call to String.Concat. Concat has different overloads. Option 1 calls Concat(string, string, string) while option 2 calls Concat(object, object, object) with two strings and a boxed int. Internally Concat then calls ToString on the boxed int.

Also, check this related question: Strings and ints, implicit and explicit

Community
  • 1
  • 1
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • Why doesn't option 2 call ToString() on the int and use the Concat(string, string, string) overload? – CJ7 May 06 '10 at 06:56
  • 1
    I would assume that it is because it makes code generation for the caller simpler by letting the Concat method do all the work. – Brian Rasmussen May 06 '10 at 07:04
1

you can use StringBuilder too:

System.Text.StringBuilder str = new System.Text.StringBuilder();
str.Append("HELLO"); 
str.Append(X); 
str.Append("World");
Rbacarin
  • 707
  • 6
  • 12