Params Parameter with default parameter values

I saw the parameter paramsmore than I can say, and always deleted it without thinking about it. Now I found out about his purpose. I just found out that the parameter paramsshould be the last in the parameter list. But this is what I learned about parameters that have a default value. Example:

MyMethod(string Name, int blah=0). 

So, the question is, do I need to specify a default value, as indicated above, when using params, can this be done? If so, what should be announced last? Example:

MyMethod(int blah=0, params string[] variableData). 

Thanks for your help again. James

+5
source share
2 answers

Your example is correct:

public void TestMethod(string name = "asdasd", params int[] items)
{
}

params must be the last, no matter which parameter is used before.

+3

, params - , .

params ( ), :

MyMethod(5, "x", "y");                            // Fine, no defaulting
MyMethod(variableData: new string[] { "x", "y"}); // Default for blah
MyMethod();                                       // Default for blah, empty variableData
MyMethod(new string[] { "x, "y" });               // Invalid   
MyMethod("x", "y");                               // Invalid
+4

All Articles