nyc1226
nyc1226

Reputation: 17

Is it possible to use the placeholder string twice?

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Type a number");
            ConsoleKeyInfo KeyInfo1 = Console.ReadKey();
            Console.WriteLine("Type another number");
            ConsoleKeyInfo KeyInfo2 = Console.ReadKey();
            Console.WriteLine("The number is {0}", KeyInfo1.KeyChar.ToString() + "The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo2.KeyChar.ToString());
            Console.ReadKey();
        }
    }
}

The reason I'm asking this question is because the first number shows up in the console but the second number doesn't show up, it just says {1}. I hope this makes sense, I'm new to programming.

Upvotes: 1

Views: 87

Answers (1)

Jite
Jite

Reputation: 5847

The first parameter of the WriteLine function is the format string.
Thats the part where you add placeholders.
You add the values to replace the placeholders as parameter after the first format parameter, like this:

Console.WriteLine("The number is {0} The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo1.KeyChar.ToString(), KeyInfo2.KeyChar.ToString());

Where the first parameter (after format, ie, second paramater passed into the function) will replace {0} and the second will replace {1}.

Upvotes: 4

Related Questions