0

还行吧:

Console.WriteLine("this is the key that has run: {0}", this.ReportKey.ToString());

这种将字符串组合在一起的方式是否仅在为控制台构建参数时可用,或者可以在其他上下文中使用。可以用在下面吗?我已经把我失败的尝试。

public string RunTheReport(){
    return "Name: " + this.ReportName " key: " + this.ReportKey.ToString();
    //return ("Name: {0} key: {1}", this.ReportName, this.ReportKey.ToString()); <<is there a way to avoid using all the "+" signs?
}
4

2 回答 2

6

使用string.Format

public string RunTheReport(){
    return string.Format("Name: {0} key: {1}", ReportName, ReportKey);
}
于 2012-11-21T16:10:35.870 回答
1
string.Format("Name: {0} Key: {1} ..", para1, para2,...);

这适用于数组,也可以有混合索引

string[] sArray = new string[]{"xx","yy","zz"};
string.Format("Index1:{1} Index0:{0} Index2:{2}",sArray);
//Output = "Index0:yy Index1:xx Index2:zz"
于 2012-11-21T16:19:20.290 回答