无法在方法定义中使用多个 params 参数的原因是什么?
类似的东西:
public static void Estimate ( params string [ ] names, params int[] numbers )
{ }
你可以在哪里使用它,例如:
Estimate ( "Eric", "Simpson", "Jon", 1, 2, 3, 4, 5 )
Something like:
public static void Estimate ( params string [ ] names, params int[] numbers )
{ }
where you could use it like:
Estimate ( "Eric", "Simpson", "Jon", 1, 2, 3, 4, 5 )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
托马斯和柯克是对的,这将很难解决,更重要的是,从使用它的开发人员的角度来看,它可能会产生意想不到的行为。例如,采用以下类:
在方法中使用“params”关键字允许多个参数将使编译正常,并且在很多情况下都可以正常工作,直到有人尝试将该类用作
Overload
(或类似的东西),然后就会发生编译错误。另一个困难是用“params”关键字标记的参数允许在调用方法时省略该参数。例如,如果在不带参数甚至使用多个 int 值调用方法的情况下,也无法充分解决以下问题。如果存在明显的好处,通常会添加语言功能。它们会让事情变得更容易还是只会阻碍进展?这是一个明显的障碍,可以通过像 Tomas 建议的那样并使用 params object[] 参数来解决。如果按类型分隔更适合您,您还可以传递单独的数组(即 SomeOtherMethod(string[] arrayOfString, int[] arrayOfInt))。
Tomas and Kirk are right, this would be difficult to resolve and, even more so, it would potentially produce unexpected behavior from the point of the developer using it. For example, take the following class:
Allowing multiple parameters using the 'params' keyword in a method would make this compile just fine, and would work fine for quite a few cases until someone tried to use the class as
Overload<string, string>
(or something similar) and then a compilation error would happen. Another difficulty is that a parameter marked with the 'params' keyword allows the omission of that parameter when calling the method. For example, the following is also impossible to resolve adequately if the method is called with no parameters or even with several int values.Language features are generally added if there is a distinct benefit to them being there. Will they make things easier or just impede progress? This is a clear impediment, and one that can be gotten around by doing like Tomas suggested and using a params object[] parameter. You can also pass individual arrays (i.e. SomeOtherMethod(string[] arrayOfString, int[] arrayOfInt)), if separating by type works better for you.
正如评论中已经讨论的那样 - 这对于编译器编写者和规范的作者来说将是一场噩梦:-)。 (C# 中的重载解析已经足够复杂)
当您在实践中需要类似的东西时,一种可能的解决方案是放弃并使用
object[]
params 参数>。这可能不是很优雅,但它允许您做您需要的事情。编译器不会检查所有使用是否正确,但可能仍然是合理的...例如,LINQ to XML 类型的构造函数(例如
XElement
)通常会这样做,这样您就可以创建 < code>XElement 包含其他元素、属性、元素集合等。As already discussed in the comments - this would be a nightmare for compiler writers and for the authors of the specification as well :-). (overload resolution in C# is already complicated enough)
One possible solution when you need something like this in practice is to just give up and use a single
params
parameter of typeobject[]
. This may not be very elegant, but it allows you to do what you need. The compiler will not check whether all uses are correct, but it may still be reasonable...For example, constructors of LINQ to XML types (e.g.
XElement
) usually do this, so that you can createXElement
containin other elements, attribtues, collections of elements, etc..