有没有办法减少使用String.Format(....,p1,p2,p3)的详细程度?

Edw*_*uay 3 c# string.format verbosity

我经常使用String.Format()它,因为它使字符串的构建更具可读性和可管理性.

有没有减少其语法冗长,例如使用扩展方法等?

Logger.LogEntry(String.Format("text '{0}' registered", pair.IdCode));

public static void LogEntry(string message)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

例如,我想使用我使用的所有方法和其他接收字符串的方法Console.Write(),例如:

Logger.LogEntry("text '{0}' registered", pair.IdCode);
Run Code Online (Sandbox Code Playgroud)

Kon*_*lph 13

怎么样:

static void LogEntry(string format, params object[] args) {
    Console.WriteLine(format, args); // For example.
}
Run Code Online (Sandbox Code Playgroud)

现在你可以像这样调用它:

Logger.LogEntry("text '{0}' registered", pair.IdCode);
Run Code Online (Sandbox Code Playgroud)