为什么C#允许方法的最后一个参数只有“可变长度"? [英] Why C# allows that only the last parameter of a method is of "variable length"

查看:55
本文介绍了为什么C#允许方法的最后一个参数只有“可变长度"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,C#允许方法的最后一个参数只有可变长度",例如:

From what I know, C# allows that only the last parameter of a method is of "variable length", for example:

T f(A a, params B[] b)允许如果您拥有A r; .... B x, y, z; ....,则可以像f (r, x, y, z)一样调用f.为什么C#也没有定义如下内容:

T f(A a, params B[] b) allows that if you have A r; .... B x, y, z; .... you can call f like f (r, x, y, z). Why C# doesn't also define something like:

T f(params A[] a, params B[] b)

推荐答案

因为编译器将如何知道第一个参数的变量参数何时停止?

Because how would the compiler know when the variable arguments for the first parameter stop?

请告诉我argOneargTwo在方法正文中应包含什么:

Pleas tell me what argOne and argTwo should contain inside of the method body:

void Foo( params object[] argOne, params object[] argTwo )
{
    // whatever
} 

Foo( 1, false, "Hello", new object(), 2.3 );

这篇关于为什么C#允许方法的最后一个参数只有“可变长度"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆