0

An overloaded method sample, for named parameter call:

internal static dynamic TestMethod(int P1a, int P1b, params dynamic[] P1c)
{
    //Sample Code
}

[Overload: P2] //illustration : Overload Specification
internal static dynamic TestMethod(int P2a, int P2b, params dynamic[] P2c)
{
    //Sample Code
}

Having a new set of code organization, is to specify which overload to use using named parameters would resolve the problem or declare the method for overload selection;

TestMethod(P2: 1, 1,2,3,4,5);
H3sDW11e
  • 188
  • 1
  • 3
  • 11
  • no, the question is, how to use named parameter with params; – H3sDW11e Oct 19 '20 at 12:24
  • `P1: new object[] { 1, 2, 3 }` – E. Shcherbo Oct 19 '20 at 12:25
  • 1
    Does this answer your question? [Named Parameters and the params keyword in C#](https://stackoverflow.com/questions/16193871/named-parameters-and-the-params-keyword-in-c-sharp) – E. Shcherbo Oct 19 '20 at 12:26
  • if to create a new object, why the params parameter then? – H3sDW11e Oct 19 '20 at 12:30
  • You can use both ways as you can see [here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params). Why are worrying about this if you end up with an array in either case? – E. Shcherbo Oct 19 '20 at 12:33
  • it is much better easy as 1,2,3 passing the values directly; that is what params is for; how to pass it with a named parameter P1: //???? – H3sDW11e Oct 19 '20 at 12:35
  • How do you expect it to be? `TestMethod(P1: 1, 2, 3)`? How does the compiler understand that `2` and `3` are for `P1` and not other positional arguments? – E. Shcherbo Oct 19 '20 at 12:37
  • had tryed P1: (1,2,3) it is identifyed as a System.Tuple; that is no way to pass it following the logical organization of the code? – H3sDW11e Oct 19 '20 at 12:38
  • Why do you need two methods with identical names and similar signatures in the first place? If you must use them as they are and can't do anything with them (change name, signature, whatever), then it's okay to pass `new object[] { 1, 2, 3 }`. Well, you said that `params` is for convenient way to pass several arguments to a method, but it's impossible cover all the cases. This is why I guess we still can pass array explicitly. – E. Shcherbo Oct 19 '20 at 12:43
  • the organization of the code sample is made by identifying "force" the required overload by using named parameter calls; it works fine until it conflicts with other overload, so by using the first one with params named parameter; – H3sDW11e Oct 19 '20 at 12:48
  • As I said it's totally fine to create an array explicitly for such rare cases. I don't think you need to worry about this. – E. Shcherbo Oct 19 '20 at 12:55
  • don't worry about the code organization? that is what the whole thing is all about; keep the logics, the issue where found at the very beginning with 2 digits; – H3sDW11e Oct 19 '20 at 12:57

1 Answers1

4

They two features are not compatible. You would have to construct the array yourself, rather than having the compiler do that for you (an array is always constructed either way).

How do we know they're not compatible? From Argument lists:

An argument with an argument_name is referred to as a named argument, whereas an argument without an argument_name is a positional argument. It is an error for a positional argument to appear after a named argument in an argument_list.

Note that last sentence - you cannot have positional arguments after a named argument.

Runtime evaluation of argument lists:

Methods, indexers, and instance constructors may declare their right-most parameter to be a parameter array

...

When a function member with a parameter array is invoked in its expanded form, the invocation must specify zero or more positional arguments for the parameter array ...

(My emphasis)

That is, the params feature only works with positional arguments.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • How do we know they're not compatible? the closest System.Tuple where placed in the logical manner of the procedure for Named Parameter; in which the named argument in the positioned named scope sample identified as System.Tuple are type as follows namedparams : (1,2,3) <- named params scope; and then the runtime finds the joker face and paint it smile at the logical procedure of a System.Tuple; – H3sDW11e Oct 19 '20 at 13:58
  • There is no "positioned named" - there are positional arguments, and there are named arguments. Params only works with positional arguments, as my final quote says. I'm linking to the language reference, I'm not sure what else you're looking for. – Damien_The_Unbeliever Oct 19 '20 at 14:01