gpt4 book ai didi

c# - 使用 C# 实现代码模板

转载 作者:行者123 更新时间:2023-11-30 14:39:53 24 4
gpt4 key购买 nike

当我需要代码模板时,我可以使用Python如下。

templateString = """
%s
%s
%s
"""

print templateString % ("a","b","c")

如何使用 C# 实现等效项?

我试过了

using System;

class DoFile {

static void Main(string[] args) {
string templateString = "
{0}
{1}
{2}
";
Console.WriteLine(templateString, "a", "b", "c");
}
}

但是我得到了

dogen.cs(86,0): error CS1010: Newline in constant
dogen.cs(87,0): error CS1010: Newline in constant
dogen.cs(88,0): error CS1010: Newline in constant

当然 templateString = "{0}\n{1}\n{2}\n"; 有效,但我需要使用多行模板,因为 templateString 用于生成一个部分代码,而且真的很长。

最佳答案

您需要在第一个 引号之前放置一个@

templateString = @"
{0}
{1}
{2}
";

让它成为 verbatim-string-literal

In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences *are not processed* in verbatim string literals. A verbatim string literal may span multiple lines.

关于c# - 使用 C# 实现代码模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5996218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com