1

Can someone explain difference in this 2 codes.

Console.WriteLine("Our total"+ total);

and

Console.WriteLine("Our total {0}", total);

Which of code is more or less correctly than other?

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Natty
  • 33
  • 1
  • 1
  • 9

4 Answers4

4

In this line

Console.WriteLine("Our total"+ total);

will always contact after left side of + sign is finish.

In this line

Console.WriteLine("Our total {0}", total);

will print where it's position is putted.

Like

Console.WriteLine("{0} is our total ", total);

This'll print first in the string.

if total is 10

Output :

10 is our total.

Both is correct but with different way of printing.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
2

Both variants are correct, and in this case practically equivalent. One uses string concatenation, where you take all the strings needed, and just + them together.

The other uses formatting, where you specify the format of the output, and provide parameters that will be replaced by the formatter.

If you have only one parameter, and you only use it once, both approaches are more or less understandable, and have, more or less, similar code lengths.

But, let's say you need to write something more specific, like a person's information in a specified format, e.g.

FirstName LastName (Age) from City, living on 'Street' #StreetNumber

using concatenation (and assuming that you have a person variable) it would look like:

string info = person.FirstName + " " + person.LastName + " (" 
            + person.Age + ") from " + person.City + ", living on '"
            + person.Street + "' #" + person.StreetNumber;

and using formatting, it would look like:

string info = string.Format("{0} {1} ({2}) from {3}, living on '{4}' #{5}",
              person.FirstName,  person.LastName, person.Age,
              person.City, person.Street, person.StreetNumber);

In the first example, the end format is really non-obvious, and it's hard to say if you've forgotten a quote or a parenthesis somewhere, and in the second case the format is relatively obvious. If you need to change the format later, i.e. if the street name/number need to go before the city, in the first case, you'd need to do some heavy editing, and in the second case, you'd just need to change the format.


C# 6.0 has string interpolation, that makes it even easier and more obvious to format strings nicely. You could just write:

string info = $"{person.FirstName} {person.LastName} ({person.Age}) from {person.City}, living on '{person.Street}' #{person.StreetNumber}";
SWeko
  • 30,434
  • 10
  • 71
  • 106
1

Console.WriteLine("Our total"+ total) is a direct print of your output.

Console.WriteLine("Our total {0}", total) would be like string.Format("Our total {0}", total).

Both are correct but the second is prefered because it ensures proper formatting and is cleaner to read/better to maintain.

Spikee
  • 3,967
  • 7
  • 35
  • 68
1

A really thorough answer is available on already on SO. In short:

I suspect other answers may talk about the performance hit, but to be honest it'll be minimal if present at all - and this concatenation version doesn't need to parse the format string.

Essentially it is up to the writer as to what he thinks is more readable.

Community
  • 1
  • 1
Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60