将范围({)写入控制台应用程序中的字符串

Sha*_*ver 5 c# string console-application

我正在尝试使用ac#console应用程序编写代码生成器.现在当我键入它时,我收到一个错误:

    Console.WriteLine("sphere {{0}{1} {2} texture{Gold_Metal}}", 
    pre, i.ToString(), sprad.ToString());
Run Code Online (Sandbox Code Playgroud)

它说"以错误的格式输入"我已经检查过所有变量都是字符串,它们是.当我尝试

    Console.WriteLine("sphere {0}{1} {2} textureGold_Metal", 
    pre, i.ToString(), sprad.ToString());
Run Code Online (Sandbox Code Playgroud)

它工作得非常好.有没有办法来解决这个问题?

pax*_*blo 4

假设您想要将一个文字{插入到流中,您需要{另一个大括号来转义您,如下所示:

Console.WriteLine("sphere {{{0}{1} {2} ...
                          ^^
                          ||-- see here.
Run Code Online (Sandbox Code Playgroud)

类似地,对于结束大括号,这里的MSDN 字符串格式常见问题解答中有详细说明。序列{{变为{}}变为}

根据我对您的意图的理解,您的具体案例的完整陈述将是:

Console.WriteLine("sphere {{{0}{1} {2} texture{{Gold_Metal}}}}", 
    pre, i.ToString(), sprad.ToString());
Run Code Online (Sandbox Code Playgroud)

这应该给你类似的东西:

sphere {<Arg0><Arg1> <Arg2> texture{Gold_Metal}}
Run Code Online (Sandbox Code Playgroud)