11

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). 

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

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
iDevJunkie
  • 777
  • 3
  • 10
  • 20
  • http://stackoverflow.com/questions/3948971/c-sharp-4-0-optional-parameters-and-params-do-not-work-together – Guy P Feb 28 '13 at 06:56
  • i think its perfectly all right to do this.did you try it. – Prabhu Murthy Feb 28 '13 at 06:59
  • 1
    Hey guys. Thanks for your comments. I've been without a computer till last night. Just was able to use my new laptop today. This is why I wasn't able to test it. So for those that don't know, default value parameters are declared last except if params is used. In the latter case, the default value parameter is declared second to last and the params parameter is declared last. Thanks!!! – iDevJunkie Mar 02 '13 at 18:43

2 Answers2

8

Your example is correct:

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

params has to be last, no matter what parameter are used before that.

Ricky
  • 10,044
  • 3
  • 26
  • 31
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Hello MarinJuraszek. Thanks for the simplicity of your answer. I just got a new laptop last night and was finally able to test my scenarios to see the results. I see you're absolutely right! – iDevJunkie Mar 02 '13 at 18:36
6

Yes, params are a special case here - they're the only situation in which a parameter without a default value can come after one with a default value.

However, you can't then call the method and take advantage of the params side of things (for a non-empty array) without also specifying the optional parameter:

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
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Fun fact: when you add default parameter values and recompile, the compiler goes round and inserts those default parameters into the calls to your method - found that out the hard way. – JerKimball Feb 28 '13 at 07:12
  • @JerKimball: Yes, it would. That's how default parameters work - they're baked into the call site. – Jon Skeet Feb 28 '13 at 08:05
  • @JonSkeet there seems te be a missing Quotation mark in 'MyMethod(new string[] { "x, "y" });' but Edit wont allow me to change it unless i edit more then 6 characters. just to let you know – Silvergerma Dec 07 '22 at 15:41