相关疑难解决方法(0)

C#中的`params`会不会导致每次调用都分配一个新的数组?

C#/ .NET通过传递Array类型by-reference来具有可变参数函数参数(与C/C++相反,它只是将所有值直接放在堆栈上,无论好坏).

在C#世界中,这有一个很好的优点,允许您使用'raw'参数或可重用的数组实例调用相同的函数:

CultureInfo c = CultureInfo.InvariantCulture;

String formatted0 = String.Format( c, "{0} {1} {2}", 1, 2, 3 );

Int32 third = 3;
String formatted0 = String.Format( c, "{0} {1} {2}", 1, 2, third );

Object[] values = new Object[] { 1, 2, 3 };
String formatted1 = String.Format( c, "{0} {1} {2}", values );
Run Code Online (Sandbox Code Playgroud)

这意味着生成的CIL相当于:

String formatted0 = String.Format( c, "{0} {1} {2}", new Object[] { 1, 2, 3 } );

Int32 third = 3;
String formatted0 …
Run Code Online (Sandbox Code Playgroud)

.net c# variadic-functions

14
推荐指数
3
解决办法
2507
查看次数

标签 统计

.net ×1

c# ×1

variadic-functions ×1