简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM