有没有办法减少使用 String.Format(...., p1, p2, p3) 的冗长性?

发布于 2024-08-31 13:39:45 字数 402 浏览 3 评论 0原文

我经常使用 String.Format() ,因为它使字符串的构建更易于阅读和管理。

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

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

public static void LogEntry(string message)
{
    ...
}

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

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

I often use String.Format() because it makes the building of strings more readable and manageable.

Is there anyway to reduce its syntactical verbosity, e.g. with an extension method, etc.?

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

public static void LogEntry(string message)
{
    ...
}

e.g. I would like to use all my and other methods that receive a string the way I use Console.Write(), e.g.:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心奴独伤 2024-09-07 13:39:45

怎么样:

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

现在你可以这样称呼它:

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

How about:

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

Now you can call it like this:

Logger.LogEntry("text '{0}' registered", pair.IdCode);
喵星人汪星人 2024-09-07 13:39:45

如果您控制 Logger.LogEntry 方法,则只需添加包含 string.format 的重载即可。只需将第二个参数声明为参数数组即可!

If you control the Logger.LogEntry method, you can simply add an overload that encompasses the string.format. Just declare the second parameter as a paramarray and you are good to go!

若沐 2024-09-07 13:39:45

是的,您可以创建一个名为 FormatWith 的扩展方法,它可以让您说出以下内容:

Logger.LogEntry("I hate my {0}".FormatWith(itemName));

自行推出应该很容易,但这里有一个示例: http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension -方法.aspx

Yes, you can make an extension method named FormatWith, which lets you say things like:

Logger.LogEntry("I hate my {0}".FormatWith(itemName));

It should be easy enough to roll your own, but here's an example: http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx

帅气尐潴 2024-09-07 13:39:45

如果 Logger.LogEntry 是您无法控制的静态方法,则不行;您只能向实例添加扩展方法。如果这是您的类型,您可以添加:

public static void LogEntry(string format, params object[] args) {
    ... string.Format(format,args) ...
}

If Logger.LogEntry is a static method outside of your control, then no; you can only add extension methods to instances. If it is your type, you could add:

public static void LogEntry(string format, params object[] args) {
    ... string.Format(format,args) ...
}
一杆小烟枪 2024-09-07 13:39:45

您可以使用 params 关键字将第一个参数之后的所有参数组合到一个数组中,并将该数组传递给 String.Format。

static void FormatString(string myString, params string[] format)
{
     Console.WriteLine(String.Format(myString, format));
}

You could use the params keyword to combine all of the arguments after the first argument into an array and pass that array to String.Format.

static void FormatString(string myString, params string[] format)
{
     Console.WriteLine(String.Format(myString, format));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文