1

Possible Duplicate:
C# String output: format or concat?

Console.WriteLine("Hi there, {0} {1}!", userName, userSurname);

Console.WriteLine("Hi there, " + userName + " " + userSurname + "!" );

i wanna know what is different between these to ways and which is better?

Community
  • 1
  • 1
Masoud Darvishian
  • 3,754
  • 4
  • 33
  • 41
  • 1
    http://stackoverflow.com/questions/296978/when-is-it-better-to-use-string-format-vs-string-concatenation – Habib Sep 19 '12 at 06:33

7 Answers7

3

For:

Console.WriteLine("Hi there, {0} {1}!", userName, userSurname);

Your first call resolves to: Console.WriteLine(string,object,object)

IN IL

 IL_000d:  ldstr      "Hi there, {0} {1}!"
 IL_0012:  ldloc.0
 IL_0013:  ldloc.1
 IL_0014:  call       void [mscorlib]System.Console::WriteLine(string,
                                                                object,
                                                                object)

For:

Console.WriteLine("Hi there, " + userName + " " + userSurname + "!" );

While your second statement makes a call to string.Concat method.

IL_0042:  call       string [mscorlib]System.String::Concat(string[])
IL_0047:  call       void [mscorlib]System.Console::WriteLine(string)

If you look at the code using ILSpy, you will see that the part where you have used + for string concatenation is replaced with call to string.Concat method

string userName = "Test";
string userSurname = "Test2";
Console.WriteLine("Hi there, {0} {1}!", userName, userSurname);
Console.WriteLine(string.Concat(new string[]
{
    "Hi there, ",
    userName,
    " ",
    userSurname,
    "!"
}));

With respect to difference in performance, I believe if there is any its going to be negligible.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Basically, this IL does two API calls to mscorlib. It stays very close to the language. Although the idea of getting IL code for this question is great and deserves its upvotes, I feel disappointed by the results. – Larry Sep 19 '12 at 18:56
1

Both are same.

At runtime, first option is resolved to create complete string.

Still First is better as its a much cleaner way of judging what string will look like finally.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
1

The first one involves the standard .NET formatting, the second one is a simple string concatenation.

I prefer the first one, because it keeps display separated from the data, and it allows me to store differents format string to do localization, for example. Here is the idea :

var EnglishFormat = "Hi there, {0} {1}!";
var FrenchFormat = "Salut tout le monde, {0} {1}!";
...

Console.WriteLine(currentLocaleFormat, userName, userSurname); 
Larry
  • 17,605
  • 9
  • 77
  • 106
1

The general String.Format vs String concatenation subject has been discussed number of times on StackOverflow, please refer to following questions (just to start with):

Community
  • 1
  • 1
tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • 3
    Wouldn't close as duplicate be more appropriate then? – Brian Rasmussen Sep 19 '12 at 16:04
  • @BrianRasmussen The question is as a duplicate of which one exactly. All above questions bring something new to the subject and none of them has been closed as a duplicate (despite the fact that there was suggestions in every one of them). – tpeczek Sep 19 '12 at 19:50
0

There is no difference in the result.

The first one uses string formatting the second one concatenates the strings.

Try to avoid concatenations because it uses more memory

Community
  • 1
  • 1
Miklós Balogh
  • 2,164
  • 2
  • 19
  • 26
0

Both of the examples will print the same result. I find that it's much more easy to see what the result should be in the first example, if the string that should be showed is long one then it could be hard to see how it will look like.

I'm always using the first example when i should print something to the user.

Jonas W
  • 3,200
  • 1
  • 31
  • 44
0

There is no difference between the two commands both output the same result.First one is better because it avoids extra empty strings concatinations.