Formated string in c# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Formated string in c#

Hi guys ! I've run into some issues while understanding the concept of formated string in basics lesson nr 4. The first examples says as follows: int x = 10 Console.WriteLine(x) And the oucome is obvious it is 10. However I dont undertand the second example: int x = 10 double x = 20 Console.WriteLine(" x = {0} , y = {1}" , x,y) And the output is displayed in the form x= 10 y=20 Why is so ? Why doesnt the console just show the value only ? Thanks in advance.

19th Oct 2017, 6:18 AM
Patryk Orlik
Patryk Orlik - avatar
1 Answer
+ 8
It's a formatted string which consist of 2 placeholder {0} and {1} and the concept is very similar to array as we count from zero. It will be quite messy if we didn't use formatted string which requires multiple concatenation:- Console.WriteLine("x = " + x + ", y = " + y);
19th Oct 2017, 8:34 AM
Zephyr Koo
Zephyr Koo - avatar