Formated string in c# | SoloLearn: Learn to code for FREE!

+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.

10/19/2017 6:18:07 AM

Patryk Orlik

2 Answers

New 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);