1

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."

  • Didn't the book mention it previously, when talking about Console? (Also, technically, there is no "Console object", since Console is a static class.) – svick Aug 17 '14 at 17:28
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.

  • This is correct. Is there a way to mark this as the right answer? – Alexander N. Aug 21 '14 at 19:19
  • @AlexanderN. next to each ansswer is a pair of arrows (up/down) and a checkbox (below the arrows?). It's hard to see on this site compared to the other StackExchange sites, and will hopefully be improved at some point. To indicate an answer is useful, click the Up arrow. To mark an aswer as your accepted answer, tick the checkbox so it goes green. You can do either or both of these actions. – Reg Edit Aug 22 '14 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.