6

Confusion in a really a basic question: I have seen in many books, they use use Console.WriteLine as:

int i = 12;
Console.WriteLine("MyVariable value is {0}", i);

Instead of

int i = 12;
Console.WriteLine("MyVariable value is" + i);

Is there any difference between them?

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • You might want to research the different ways to format a String. The answer to your question there is no difference between the two, one is just cleaner then the other, and easier to read. – Security Hound Sep 29 '11 at 13:10
  • 1
    possible duplicate of [C# String output: format or concat?](http://stackoverflow.com/questions/16432/c-string-output-format-or-concat) – BrokenGlass Sep 29 '11 at 13:10

5 Answers5

8

in your example, practically not. However, the first case can easily be extended to do

Console.WriteLine("MyVariable value is {0} and myothervar is {1}", i, j);

which could be a little cumbersom with the second approach.

Rickard
  • 2,325
  • 13
  • 22
  • but overall they are same, in terms of speed and performance, right? – Sandy Sep 29 '11 at 13:15
  • @rapsalands - You shouldn't worry about the performance of String. There isn't anything you can do about any performance loss, even if you could blame String, which isn't going to be the case. Unless you have a performance problem do not worry about using something that will give you 0.0001% better performance. – Security Hound Sep 29 '11 at 13:19
  • 3
    @rapsalands: think about it this way: **You are writing to the console**. The console is going to be read by the user. The screen is only updated about every 10 ms at best and the user's reaction time is far slower than that. You really think it matters to the user reading the console whether it takes 0.0001 ms or 0.0002 ms to render the string? Spend your valuable time optimizing something that the user could possibly notice. – Eric Lippert Sep 29 '11 at 13:29
  • @Eric: We were also writing to a console (and a file). Many lines of debug information per second, a performance hit we were willing to take during development. But we were not willing to take it when the product was to be deployed to production and debug logging was turned off. Unfortunately for us, we were still `string.Format`ing our debug log entries, even though we were not printing them to the console/file, which caused a huge performance hit. Fortunately for us, it was caught during QA load-testing. I believe keeping in mind that string operations *do* cost something is important. – Allon Guralnek Sep 29 '11 at 15:03
  • 1
    @AllonGuralnek: Good point! And one which illustrates the importance of both (1) performance testing before you ship, and (2) using tools to discover what the real performance-impacting code is. – Eric Lippert Sep 29 '11 at 15:08
3

Maybe this will help someone in the future. There is now a 3rd method(Interpolation) and it is the cleanest of them all! They are all just different ways of writing the same thing.

int i = 12;

// Interpolation Method- Req. C# 6 or later [Cleanest]
Console.WriteLine($"MyVariable value is {i}");

// Concatenation Method (from VB days)
Console.WriteLine("MyVariable value is " + i); 

// Format Method (from C days)
Console.WriteLine("MyVariable value is {0}", i);
SunsetQuest
  • 8,041
  • 2
  • 47
  • 42
3

Check out the answer in this thread. In a simple case it doesn't really matter, but there are performance considerations if you are doing this in a large loop or something.

Community
  • 1
  • 1
skaz
  • 21,962
  • 20
  • 69
  • 98
1

I believe you see the string concatenation in books for beginner because it is simple. String formatting needs to be explained before it can be used in such simple example. String concatenation is much simple and may already have been taught at that point in the book (even if not, it is simple enough to learn by example).

I can see how C programmers might be confused by the format syntax when encountering it for the first time. When attempting to extend it to two variables they might think it needs to be written as so:

int numA = 3;
int numB = 5;
Console.WriteLine("numA is {0} and numB is {0}", numA, numB);

Thinking it would be similar to C's printf where {0} is equivilant to %d:

printf("numA is %d and numB is %d", numA, numB);

They would of course be surprised to have varA be printed twice. Or they may be frustrated of not knowing the equivalent of %s when trying to Console.WriteLine a string. String concatenation, on the other hand, has fewer pitfalls and can be more easily extended by beginners. Of course it is more cluttered but also more powerful, power that may be confusing in a book introducing someone to C# since string formatting syntax can grow quite complex:

Console.WriteLine("numA is {0,17:$00.00####}", numA);

The above example also illustrates the difference between concatenation and string formatting. They are different, but for simple usages like the one you made, it makes little difference.

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
0

Your specific case will not, for more information

http://msdn.microsoft.com/en-us/library/ms228362.aspx

Regards