Reputation: 51
So I'm in the process of learning C#. Going through Tabors MSDN videos. I'm kind of confused on to why this outcome is what it is. I don't understand what the {1} and {0} are, and I don't understand if he is trying to subtract them. In my head, what should come out when using readline would be "(not sure), 2, car". The userValue is et at 2, so the message is not equal to 1 which makes it equal to car. It just clicked as I'm typing this. {1} is referring to message, the second variable in the statement, while {0} is referring to userValue, the first variable. Therefore the outcome is "car - 2". I'm pretty sure I just figured it out but can someone confirm. Thank you.
int userValue = 2;
string message = (userValue == 1) ? "boat" : "car";
Console.WriteLine("{1} - {0}", userValue, message);
Console.ReadLine();
Upvotes: 1
Views: 686
Reputation: 1
{1} is referring to message, the second variable in the statement, while {0} is referring to userValue, the first variable. Therefore the outcome is "car - 2". I'm pretty sure I just figured it out but can someone confirm.
string message = (userValue == 1) ? "boat" : "car";
Console.WriteLine("{1} - {0}", userValue, message);
But if you change places of trailing parameters e.g.
Console.WriteLine("{0} - {1}", userValue, message);
Or places of variables,
Console.WriteLine("{1} - {0}", message, userValue);
You get:
2 - car
Upvotes: 0
Reputation: 11480
The reason they are utilizing the {0}
and etc. is for an index or placeholder, this is helpful as without said code, you would end up with the following:
Console.WriteLine(userValue + " " + message);
So, with the above approach you've utilized concatenation. This will conjoin your two values together, in the case of a String
it is considered immutable which means every time you concatenate your impeding memory. So if you do:
string message = String.Empty;
message += userValue;
message += note;
The +=
that is an event, which adds our current value plus a new value. The thing to note though, is that with each message =
it recreates the value in memory, so larger data can become a problem.
The built in function to Console.WriteLine
best correlates to String.Format
. Which in essence would allow us to do the following:
var message = String.Format("{0} {1}", userValue, note);
So when you utilize said holder, it builds our output like the concatenation but in a more performant and easier approach to read. Since you must start with {0}
it makes it incredibly easy to know which value you'd like to modify as well.
Important: To keep the example easier to understand, but the index
or placeholder
comparison, is for understanding but they're parameters that will populate said position based on the order of the implementation.
An example with your code:
var userValue = 2;
var message = String.Empty;
if(userValue == 1)
message = "boat";
else { message = "car"; }
For simplicity I removed your ternary, because you hard coded the user value your statement will never test true, as you have it set to 2
. So you have two values, as you can denote if userValue == 1
do that, else assign message your other variable.
When you do this part:
Console.WriteLine("{0} - {1}", message, userValue);
Your converting both parameters into a string
, but it won't do any subtraction because a string
doesn't do mathematical functions, so you would end up with an output of:
car - 2;
The reason is, as I denoted earlier both your parameters are acting as a placeholder. So the value of those two parameters will be inserted into your String
.
Hopefully this outlines what it is doing better for you.
Upvotes: 4
Reputation: 49
Actually, the Console.WriteLine
can take more than 1 arguments, which one would be the text. Since that, you can reference the other variables inside the text with {n}
inside it, where n
is the position of the argument. So, when he says Console.WriteLine("{1} - {0}", userValue, message);
he actually gets "car - 2" as return.
Upvotes: 0
Reputation: 7097
Those strings that look like "{0}"
are format string parameters that get filled in by the trailing variables. The number indicates which variable to use starting with 0.
For example:
Console.WriteLine("{1} - {0}", userValue, message);
//prints "car - 2"
This is because {0}
is userValue
which equals 2. It's index is 0 since it's the first in the list following the format string. Then {1}
is message
which the expression posted would be "car" since userValue == 2
.
Upvotes: 1