问题描述
我见过 params
参数的次数比我说的要多,而且总是在不考虑其含义的情况下将其删除.现在我已经了解了它的目的.我刚刚了解到的是 params
参数必须是参数列表中的最后一个.但这是我对指定默认值的参数的了解.示例:
I've seen the params
parameter more times than I can say and always removed it without thinking about it's meaning. Now I've learned its purpose. What I just learned is that the params
parameter must be the last in the parameter list. But this is what I learned about the parameters that have a default value specified. Example:
MyMethod(string Name, int blah=0).
所以问题是如果我需要在需要使用 params
的同时指定一个默认值,可以这样做吗?如果是这样,哪个必须最后声明?示例:
So the question is if I need to specify a default value as above while needing to use params
, can this be done? If so, which must be declared last? Example:
MyMethod(int blah=0, params string[] variableData).
再次感谢您的帮助.詹姆斯
Thanks for your help again. James
推荐答案
你的例子是正确的:
public void TestMethod(string name = "asdasd", params int[] items)
{
}
params
必须在最后,无论之前使用什么参数.
params
has to be last, no matter what parameter are used before that.
这篇关于params 具有默认参数值的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!