none
Named arguments can't be used with params modifier RRS feed

  • Question

  • Say, I have the following code:

    void testFunc()
    {
    	AddNumbers("dic1", 1, 2, 3);
    	AddNumbers(name: "dic2", ints: 1, 2, 3);
    	void AddNumbers(string name, params int[] ints) { }
    }

    The first call is OK. But when I try to use named arguments (name and ints in second call), I get the following error:

    No overload for method 'AddNumbers' takes 4 argument.

    I wonder why do I get this error?

    What's more interesting the following code works OK:

    AddNumbers(name: "dic2", ints: 1);

    There is no knowledge that is not power.


    • Edited by JohnyL Friday, June 22, 2018 1:40 PM Corrected token
    Friday, June 22, 2018 9:29 AM

Answers

  • Dave's explanation is correct, but I think it's worthwhile to explore why this is an issue.

    "params" does create an array, but it only creates an array from any unlabeled parameters left over in the argument list after everything else has been assigned, just like varargs does for C and C++.  The named parameter syntax matches one argument with one name.  So. when you wrote this:

       AddNumbers(name: "dic2", ints: 1, 2, 3 );

    the compiler parses it like this:

        AddNumbers(  {  name: "dic2" }  ,  { ints: 1 } ,  2,  3 };

    It assigns "dic2" to "name", then attempts to assign "1" to "ints".  That leaves it with two additional arguments, but it has already assigned values to all of the parameters, so there's nowhere else to put 2 and 3.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by JohnyL Friday, June 22, 2018 8:08 PM
    Friday, June 22, 2018 7:27 PM

All replies

  • Try this:

    AddNumbers(name: "dic2", ints: new int[] {1, 2, 3});


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Friday, June 22, 2018 1:31 PM
  • @Dave Doknjas

    Thanks for answer! Yes, I could do it this way, but I just wonder why I can't get without array? params should implicitly create an array. It does so without naming argument, but fails with it. Any thoughts?


    There is no knowledge that is not power.

    Friday, June 22, 2018 1:39 PM

  • Thanks for answer! Yes, I could do it this way, but I just wonder why I can't get without array? params should implicitly create an array. It does so without naming argument, but fails with it. Any thoughts?

    I think it's because the value for a named parameter ends on the first comma encountered at the same bracket level.  This might be a small syntax shortcoming, but the work-around is simple.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Proposed as answer by Tim Roberts Friday, June 22, 2018 7:27 PM
    Friday, June 22, 2018 1:46 PM
  • Dave's explanation is correct, but I think it's worthwhile to explore why this is an issue.

    "params" does create an array, but it only creates an array from any unlabeled parameters left over in the argument list after everything else has been assigned, just like varargs does for C and C++.  The named parameter syntax matches one argument with one name.  So. when you wrote this:

       AddNumbers(name: "dic2", ints: 1, 2, 3 );

    the compiler parses it like this:

        AddNumbers(  {  name: "dic2" }  ,  { ints: 1 } ,  2,  3 };

    It assigns "dic2" to "name", then attempts to assign "1" to "ints".  That leaves it with two additional arguments, but it has already assigned values to all of the parameters, so there's nowhere else to put 2 and 3.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by JohnyL Friday, June 22, 2018 8:08 PM
    Friday, June 22, 2018 7:27 PM
  • @Tim Roberts

    Thanks, Tim, for thorough explanation! Now I got it :)


    There is no knowledge that is not power.

    Friday, June 22, 2018 8:21 PM