+ 2

I understand the code, but why are we using the placeholder ({0}) instead of the variable name?

31st Jan 2017, 11:53 PM
VoydAngel
4 Answers
+ 1
yes, but why not just use String.Format("Your number is " + intYourNumber +"."); ? why use a placeholder at all?
3rd Feb 2017, 6:20 PM
VoydAngel
+ 1
it's just another unique way of writing this code . example static void Main(string[] args) { string yourName; Console.WriteLine("What is your name?"); yourName = Console.ReadLine(); Console.WriteLine("Hello {0}", yourName); } In this scenario, we cannot use the variable name, but the input data.
3rd Mar 2017, 10:17 AM
Martins Akoje
Martins Akoje - avatar
0
All of the other comments are correct, but in case you're wondering why we use 0 instead of 1? I hope this chart below might help. Console.WriteLine("Hello {0}", yourName); could be written as Console.WriteLine("Some Generic Text {0}, {1}, {2}, {3}", a, b, c, d); In General you starting counting at 0 and NOT 1. so for every variable that comes after string: a={0}, b={1}, c={2}, d={3} etc.... You use {0} because that is the start number of an array i believe. I'm sure someone much smarter than me can get into why you start with 0 instead of 1, but that is the rule in this case. Hope this helps explain it a little further
17th Mar 2017, 12:24 AM
Marc Nardangeli
Marc Nardangeli - avatar