【发布时间】:2014-04-27 08:27:34
【问题描述】:
可能重复:
When is it better to use String.Format vs string concatenation?
在 C# 中使用 String.Format 时的最佳实践是什么。我总是在这里感到困惑,因为这样做似乎更简单
private string _ExecuteCommand(string cmd)
{
// Start the child process.
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
// Redirect the output stream of the child process.
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = String.Format("/C {0}", cmd); // prevents the command line from staying open after execution
//... you get the idea
}
使用 String.Format 与仅使用字符串连接相比有什么特别的优势吗?换句话说,我可以这样做......,
startInfo.Arguments = "/C " + cmd; // prevents the command line from staying open after execution
我想继续追求质量代码,但我不知道它的quality 何时使用String.Format。如果有人有任何建议,我将不胜感激。
【问题讨论】:
-
嗨,这会有所帮助:stackoverflow.com/questions/296978/…欢呼!
-
抱歉!我在 SO 上找过这个,我一定错过了。