Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串模板传递给函数c#_C#_String - Fatal编程技术网

将字符串模板传递给函数c#

将字符串模板传递给函数c#,c#,string,C#,String,如何将字符串模板传递给函数 我正在创建通用库 此时,主应用程序需要为电子邮件提供模板,库必须在电子邮件中的特定位置添加特定值 void SomeFunction(string Template) { string OtherString = "This text is inserted"; string result - how to set the value of this string - Some text This text is inserted aa? } st

如何将字符串模板传递给函数

我正在创建通用库

此时,主应用程序需要为电子邮件提供模板,库必须在电子邮件中的特定位置添加特定值

void SomeFunction(string Template)
{
   string OtherString = "This text is inserted";


   string result - how to set the value of this string - Some text This text is inserted aa?
}


string Template = "Some text {need insert here} aa";

SomeFunction(Template);

试着这样做:

string otherString = "inserted value";
string template = string.Format("Some text {0} aa", otherString);

试着这样做:

string otherString = "inserted value";
string template = string.Format("Some text {0} aa", otherString);
您正在寻找:

void SomeFunction(string templateString)
{
   string otherString = "This text is inserted";


   string result = string.Format(templateString, otherString);
}


string template = "Some text {0} aa";

SomeFunction(template);

但是,为什么您要这样做,而不是由提供更简单、更直接的选项

如果您将错误的templateString传递给SomeFunction,例如传递“我的测试:{0},{1}”,而SomeFunction只需要“我的测试:{0}”,该怎么办?

您在寻找:

void SomeFunction(string templateString)
{
   string otherString = "This text is inserted";


   string result = string.Format(templateString, otherString);
}


string template = "Some text {0} aa";

SomeFunction(template);

但是,为什么您要这样做,而不是由提供更简单、更直接的选项

如果将不正确的templateString传递给SomeFunction,例如传递“我的测试:{0},{1}”,而SomeFunction只需要“我的测试:{0}”,该怎么办?

检查此项:检查此项: