Replaceable parameter syntax for the console object in C#.
I am taking the O’Reilly C# Course 1 and it is asking for a replaceable parameter syntax and it is not very clear on what that means. Currently I used this:
double trouble = 99999.0009;
double bubble = 11111.0001;
Console.WriteLine(trouble * bubble);
Am I missing the meaning of replaceable parameter syntax?
Can someone provide an example for what I am looking for?
Original question for the quiz:
“Create two variables, both doubles, assign them numbers greater than 10,000, and include a decimal component. Output the result of multiplying the numbers together, but use replaceable parameter syntax of the Console object, and multiply the numbers within the call to the Console.WriteLine() method.”
1
You’re quite right that it’s not very clear, and the reason it’s not very clear is that O’Reilly are taking liberties with their terminology. It’s the kind of thing that only makes sense if you already know what they mean. And this is a course?
Probably the most commonly used overload of string.Format
is the one that takes two parameters: a format string and an object array. You’ve probably used it to format a string with data from other variables:
int myNum = 123;
string message = string.Format("The number is {0}", myNum);
One of the Console.WriteLine()
overloads takes these same two parameters, and for the same purpose:
Console.WriteLine("The number is {0}", myNum);
Microsoft tell us these markers in the string ({0}
, {1}
etc) are
indexed placeholders, called format items, that correspond to an
object,
and that the formatting process
replaces each format item with the text representation of the value of
the corresponding object.
So when O’Reilly said
Output the result of multiplying the numbers together, but use
replaceable parameter syntax of the Console object, and multiply the
numbers within the call to the Console.WriteLine() method.
what they actually meant was
Output the result of multiplying the numbers together. Perform the
calculation within a call to the Console.WriteLine() method, using an
overload that formats the output according to a string parameter
containing a placeholder for the result value.
2