为什么存在 String.Format 的重载?
我使用 Reflector 来查看 String.Format 的实现,并且一直有这样的印象: String.Format 的重载需要 1, 2 & 。 3 个参数是采用对象数组的方法的优化版本。然而,我发现它们在内部创建一个对象数组,然后调用一个采用对象数组的方法。
1 arg
public static string Format(string format, object arg0)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0 });
}
2 args
public static string Format(string format, object arg0, object arg1)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0, arg1 });
}
3 args
public static string Format(string format, object arg0, object arg1, object arg2)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0, arg1, arg2 });
}
对象数组
public static string Format(string format, params object[] args)
{
if ((format == null) || (args == null))
{
throw new ArgumentNullException((format == null) ? "format" : "args");
}
return Format(null, format, args);
}
在内部,它们最终都使用相同的代码,因此使用 1 , 2 & 3 个参数版本并不比对象数组版本快。
所以我的问题是 - 为什么它们存在?
当您使用带有逗号分隔值列表的对象数组版本时,编译器会自动将参数转换为对象数组,因为 params/ParamArray 关键字本质上就是 1, 2 & 3个版本都有,所以它们看起来是多余的。为什么 BCL 设计者要添加这些重载?
I was using Reflector to look at the implementation of String.Format and had always been under the impression that the overloads of String.Format that took 1, 2 & 3 arguments were optimized versions of the method that takes an object array. However, what I found was that internally they create an object array and then call a method that takes an object array.
1 arg
public static string Format(string format, object arg0)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0 });
}
2 args
public static string Format(string format, object arg0, object arg1)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0, arg1 });
}
3 args
public static string Format(string format, object arg0, object arg1, object arg2)
{
if (format == null)
{
throw new ArgumentNullException("format");
}
return Format(null, format, new object[] { arg0, arg1, arg2 });
}
Object array
public static string Format(string format, params object[] args)
{
if ((format == null) || (args == null))
{
throw new ArgumentNullException((format == null) ? "format" : "args");
}
return Format(null, format, args);
}
Internally they all end up using the same code and so using the 1, 2 & 3 argument versions are no faster than the object array version.
So my question is - why do they exist?
When you use the object array version with a comma separated list of values, the compiler automatically converts the arguments into an object array because of the params/ParamArray keyword which is essentially what the 1, 2 & 3 versions do, so they seem redundant. Why did the BCL designers add these overloads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Hans 提到的,原因之一是在格式化字符串的大多数常见情况下,创建数组会产生大量不必要的开销。这可以节省 EXE 中的空间。
另一个原因是并非所有语言都支持可变参数函数(在 C# 中使用
params
)。这使得这些语言的用户可以避免在最常见的字符串格式化情况下创建数组。这对于没有简单数组创建和初始化语法的语言来说可以节省很多。One reason, as Hans mentions, is that creating an array is a lot of unnecessary overhead in most common cases of formatting a string. This saves space in the EXE.
Another reason is that not all languages support variadic functions (use of
params
in C#). This allows users of those languages to avoid array creation for the most common cases of string formatting. This saves a lot for languages that don't have simple syntax for array creation and initialization.您忘记了应用程序中拨打电话所需的代码。创建数组并填充它比仅仅传递 3 个参数需要更多的 IL。
You are forgetting about the code in the app required to make the call. Creating the array and filling it takes a lot more IL than just passing 3 args.