从带参数的方法调用带参数的方法
我想做下一步:
void SomeMethod ()
{
ParamMethod1 (1,2,3);
}
void ParamMethod1 (params object[] arg)
{
ParamMethod2 (0, arg); //How to call ParamMethod2 in order to it works
//as I want
}
void ParamMethod2 (params object[] arg)
{
//I want 'arg' contains 0,1,2,3 instead of 0, System.object[]
}
感谢您的帮助。
I want to do the next:
void SomeMethod ()
{
ParamMethod1 (1,2,3);
}
void ParamMethod1 (params object[] arg)
{
ParamMethod2 (0, arg); //How to call ParamMethod2 in order to it works
//as I want
}
void ParamMethod2 (params object[] arg)
{
//I want 'arg' contains 0,1,2,3 instead of 0, System.object[]
}
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 LINQ 来完成此操作,但您仍然需要 ToArray()
编辑添加:
根据有关性能的评论,我在建议的方法之间进行了一些非常基本的性能比较。
对于 100 万次调用
当然,如果您将 object 更改为 int 以及其他一些东西,您将进一步提高性能。就我个人而言,我会使用 LINQ,因为它对于 99% 的情况来说足够快,而在其他情况下,我一开始就不会使用参数。
You can do it with LINQ but you need still need ToArray()
Edited to Add:
Based on the comment regarding performance I did some very basic performance comparisons between the suggested methods.
For 1 million calls
Of course if you changed object to int and a few other things you will improve performance further. Personally I'd use LINQ because it is fast enough for 99% of cases and in the other case I wouldn't use params in the first place.
一种方法虽然不一定理想,但可以简单地是:
也就是说,无论如何完成,我们都需要将
0
和arg
的内容组合成一个单一集合 - 毫无疑问会有一种更有效的方法,甚至可能是一种更简单的方法,我今天早上因为缺乏咖啡而看不到这种方法,但是可以这么说,这可以解决问题。One way, though not necessarily ideal, could simply be:
That is to say, regardless of how this is done, we need to combine
0
and the contents ofarg
into a single collection - there will doubtless be a more efficient way, and perhaps even a simpler approach that I can't see for lack of coffee this morning, but this will do the trick, so to speak.改变这个方法
到
这就是您所要求的。
请注意,如果您想要一个仅使用 params 作为参数的方法,则会遇到问题,因为您的编译器总是会调用此显式第一个参数签名,而不是仅调用 params 签名方法。
Change this method
To
Hope this is what your asking for.
Note here that you will have a problem if you want to have a method with only params as arguments, because always your compiler will call this explicit first argument signature than params only signature method.