C# WriteLine Concatenation Questions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C# WriteLine Concatenation Questions

Greetings All, I was playing around with the C# WriteLine Concatenation after Lesson 4.1 of the C# Course. I encountered a few weird observations I cannot understand. For the full code of my practice sheet see: https://code.sololearn.com/cA6a2157A141 First: (Example 5) in code) Console.WriteLine("a = ", a + " ; b = " + b); This gives as output: "a = ". Question: Why doesn't it write beyond the 'a = ', or give an error instead? Note: Console.WriteLine("a = ", + a + " ; b = " + b); gives the same output: why? Second: (Example 6) in code) int x = 3; int y = x++; Console.WriteLine("x = {0}", x + " ; y = " + y); This gives as the correct output: "x = 4 ; y = 3". Questions: Why does this work without the y = {1} placeholder? Why doesn't it skip the y=, the same as the a & b example 5) above? I would be grateful for any help. Thanks in advance, Erwin!

6th Jun 2021, 6:21 PM
E.J. Ten Meer
3 Answers
+ 5
Example 5: remove the comma after "a="
6th Jun 2021, 7:21 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
And, that worked, indeed.... It was that simple huh?!? :D... Thanks @Rik Wittkopp
6th Jun 2021, 8:12 PM
E.J. Ten Meer
0
I understand the error better now. The first code I had written had an error in the concatenation (+ was missing): Console.WriteLine("a = " a + " ; b = " + b); This gave an error stating 'Comma expected', so I added a comma, which led to the incomplete output. The correct code needed a plus, not a comma: Console.WriteLine("a = " + a + " ; b = " + b);
7th Jun 2021, 2:47 PM
E.J. Ten Meer