而params方法重载的好处

而params方法重载的好处

本文介绍了在C#中有两个具体的参数,而params方法重载的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多个实例在.NET框架,其中有多个重载的方法,其中的一些使用的参数接着是最终的捕获所有一定数量其中 PARAMS 关键字被使用。常见的例子是在字符串类,例如:

There are a number of examples in the .NET framework where there are multiple overloads for a method, some of which use a specific number of parameters followed by a final "catch all" where the params keyword is used. Common examples of this are on the String class e.g.:

  • String.Format()
  • String.Concat()

我在想,如果有一个,为什么有这么多的这些方法重载的一个特别的原因?起初我以为这可能是是与性能;这个问题,并回答此等问题 - 使用PARAMS在C#成本> - 建议这样

I was wondering if there is a particular reason for why there are so many of these method overloads? At first I thought it might be something to do with performance; the question and answers to this SO question - Cost of using params in C# - would suggest so.

不过,我开始钻研使用参考源网站上的.NET源$ C ​​$ C。我在 String类的源$ C ​​$ C 注意到了这一点:

However, I started to delve into the .NET source code using the Reference Source website. I noticed this in the String class source code:

String.Concat()实际运行不同的code取决于有多少固定的使用参数 - 这在我脑海里一定会优化。 的String.Format()但似乎只是提供围绕主参数方法的封装 - 请参阅下面的转述code:

String.Concat() actually runs different code depending on how many fixed arguments are used - this in my mind would definitely be an optimization. String.Format() however just seems to provide wrappers around the main param method - see below for the paraphrased code:

public static String Format(String format, Object arg0)
{
    return Format(format, new Object[] { arg0 });
}

public static String Format(String format, Object arg0, Object arg1)
{
    return Format(format, new Object[] { arg0, arg1 });
}

public static String Format(String format, Object arg0, Object arg1, Object arg2)
{
    return Format(format, new Object[] { arg0, arg1, arg2 });
}

public static String Format(String format, params Object[] args)
{
    // Do work here...
}

那么,有没有性能优势,也可以这仅仅是为了方便起见,或者两者兼而有之?在特定的情况下,上面我没有看到任何明显的好处,它只是似乎重复的工作。

So are there performance benefits or can this simply be a matter of convenience, or maybe both? In the particular case above I do not see any obvious benefit and it just seems to duplicate work.

推荐答案

更新:它不是String.Concat但的String.Format

我想这背后的原因是,所有通话结束在StringBuilder.AppendFormat这是相当复杂的,这将是code复制的主要来源,如果你会处理以不同的方式输入参数的每一个数字。

I guess the reason behind this is that all calls end up in StringBuilder.AppendFormat which is quite complex and it would be a major source of code duplication if you would handle every number of input arguments in a different way.

事情是不同的,如果你有一个重型API(如跟踪),其中通过PARAMS重载调用一个方法的开销非常显著。它可高达5倍。这同样适用于隐委托分配这是很常见的LINQ。这些临时委托实例并不便宜优化的code。罗斯林例如已禁止,因为高隐委托分配成本的LINQ的使用。

Things are different if you have an heavy duty API (like tracing) where the overhead of calling a method via a params overload is very significant. It can be up to a factor of 5.The same goes for implicit delegate allocations which is common with LINQ. These temp delegate instances are not cheap in optimized code. Roslyn for example has banned LINQ usage because of the high implicit delegate allocation costs.

这篇关于在C#中有两个具体的参数,而params方法重载的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 10:18