jp2*_*ode 1 c# string extension-methods
我在C#代码中使用String.Format可能比if声明更重要.
string ask = String.Format("Continue using [{0}]?", value);
Run Code Online (Sandbox Code Playgroud)
我只想到我经常使用它.
ask.Format("Continue using [{0}]?", value);
Run Code Online (Sandbox Code Playgroud)
所以,我得看看String.Format,并且它有很多重载,因为有许多方法可以调用它.
嗯......这使事情变得复杂.
System.String命名空间中已经存在的重载之上?以下是我想看到的内容:
public static class Extensions
{
public static String Format(this String str, String formatText, /* What goes here? */)
{
return str.Format(formatText, /* Magic */);
}
}
Run Code Online (Sandbox Code Playgroud)
我想我可以写一个重载来匹配每个String.Format重载,但这可能没有必要.
Mik*_*ole 16
ask.Format("Continue using [{0}]?", value);
Run Code Online (Sandbox Code Playgroud)
不是我会如何使用它.这就是我要做的事情:
var ask = "Continue using [{0}]?".FormatWith(value);
Run Code Online (Sandbox Code Playgroud)
这是我的扩展方法:
public static string FormatWith(this string value, params object[] args)
{
return String.Format(value, args);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5073 次 |
| 最近记录: |