Why {0} | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why {0}

static void Main(string[] args) { string yourName; Console.WriteLine("What is your name?"); yourName = Console.ReadLine(); Console.WriteLine("Hello {0}", yourName); } The code has {0}. What for?

19th Feb 2019, 10:14 PM
MoNaK
MoNaK - avatar
2 Answers
+ 2
Including the {0} within the string will just replace it with whatever variable is given after the string which will be printed out, replacing it in this case to whatever the user enters as their name (ie. It will print "Hello" + your name). The 0 in the curly brackets just tells the function which variable should replace that part of the string, as if you had more you could replace it to {2} for example to output the third variable within that function.
19th Feb 2019, 10:58 PM
Faisal
Faisal - avatar
0
Faisal Thank you very much!
21st Feb 2019, 12:31 PM
MoNaK
MoNaK - avatar