Why {0} is needed in this? Console.WriteLine("Hello {0}", name); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why {0} is needed in this? Console.WriteLine("Hello {0}", name);

7th Dec 2016, 2:25 AM
Yanar Mon
Yanar Mon - avatar
5 Answers
+ 9
Console.WriteLine() supports placeholders. You can write as many placeholders as you want in the text, and then you list the variables that go in those spaces. It generally looks neater than concatenating everything. Ex: Console.WriteLine("{0} {1} {2}", x, y, z); //as opposed to Console.WriteLine(x + " " + y + " " + z);
7th Dec 2016, 2:37 AM
Tamra
Tamra - avatar
+ 5
You're welcome! ^_^
8th Dec 2016, 7:33 AM
Tamra
Tamra - avatar
+ 1
its not limited to Console.WriteLine( ); 'substitution arguments' are a feature of the c# language and work in many methods
8th Dec 2016, 5:10 PM
Dainichi Daigoro
Dainichi Daigoro - avatar
+ 1
They are placeholders for the variables, see example below. string name = "Malachi"; int age = 24; Console.WriteLine("My name is {0}, and my age is {1}.", name, age); This will result in; My name is Malachi, and my age is 24. {0} is replaced with the variable called "name" {1} is replaced with the variable called "age". Hope this helps!
20th Dec 2016, 1:28 AM
Eris Rose
Eris Rose - avatar
0
thank you Tamra
8th Dec 2016, 7:32 AM
Yanar Mon
Yanar Mon - avatar