+ 2
I understand the code, but why are we using the placeholder ({0}) instead of the variable name?
4 ответов
+ 1
yes, but why not just use String.Format("Your number is " + intYourNumber +"."); ? why use a placeholder at all?
+ 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.
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