+ 2

Am I understanding placeholders right? Why do I still need "yourName"?

So, hi. I'm Oliver, and I just got this app after some people recommended me it. I'm a total newbie to code, if you couldn't guess. So, for the past 30 minutes, I've been struggling to understand placeholders. I'm 99% certain I understand variables, but placeholders are at a complete lost to me. So, this is the way I rationalized the concept: Values to be used in the string are somewhat like a numbered list starting at 0. Like, #0: int = 89 #1: double = 27 And so on. And, that seemed to work. But, I got to a string of code that broke that reality, and now I'm back to square one and don't get it. In the code {0} and yourName are needed in the Console.WriteLine line in order to run it! So, is my analogy to lists correct? Or am I confused? Sorry for being a total noob at code, but your help would be great! (Here's the code) string yourName; Console.WriteLine("What is your name?"); yourName = Console.ReadLine(); Console.WriteLine("Hello {0}", yourName);

8th Mar 2019, 9:26 PM
Oliver
2 Respostas
+ 2
Yes you are. {0} is pointing at the first element behind the "," which is yourName It does not matter whether the variable is a string, integer, or double. In this example {0} are both need, but the are several ways to do it. Console.WriteLine("Hello" + yourName); is also valid.
8th Mar 2019, 9:33 PM
sneeze
sneeze - avatar
+ 2
you could also write Console.WriteLine($”Hello” {yourName});
9th Mar 2019, 2:21 PM
JOHN SASSER
JOHN SASSER - avatar