code ++模板实现用C# [英] Code template implementation with C#
问题描述
当我需要code ++模板,我可以使用Python如下:
When I need code template, I can use Python as follows.
templateString = """
%s
%s
%s
"""
print templateString % ("a","b","c")
我如何实现相当于用C#?
How do I implement the equivalent with C#?
我试过
using System;
class DoFile {
static void Main(string[] args) {
string templateString = "
{0}
{1}
{2}
";
Console.WriteLine(templateString, "a", "b", "c");
}
}
不过,我得到了
But I got
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是用于生成code的一部分,这是很长的。
Of course templateString = "{0}\n{1}\n{2}\n";
works, but I need to use multiple line template as the templateString is for generating a part of code, and it's really long.
推荐答案
您需要将 @
在首先报价
you need to place an @
before the first quote
templateString = @"
{0}
{1}
{2}
";
使它成为 verbatim-字符串字面
make it a verbatim-string-literal
在一个逐字字符串中, 分隔符之间的字符 除preTED一字不差,只 例外是 报价转义序列。尤其是, 简单转义序列和 十六进制和统一code转义 序列的*不*处理中 逐字字符串。 逐字 字符串可以跨多 行。
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.
这篇关于code ++模板实现用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!