Is there a way to distingish myFunc(1, 2, 3) from myFunc(new int[] { 1, 2, 3 })?

后端 未结 6 1634
暖寄归人
暖寄归人 2020-12-11 03:08

A question to all of you C# wizards. I have a method, call it myFunc, and it takes variable length/type argument lists. The argument signature of myFunc itself is my

6条回答
  •  没有蜡笔的小新
    2020-12-11 03:51

    This is not possible in C#. C# will replace your first call by your second call at compile time.

    One possibility is to create an overload without params and make it a ref parameter. This probably won't make sense. If you want to change behavior based on the input, perhaps give the second myFunc another name.

    Update

    I understand your issue now. What you want is not possible. If the only argument is anything that can resolve to object[] it's impossible to distinguish from this.

    You need an alternative solution, maybe have a dictionary or array created by the caller to build up the parameters.

提交回复
热议问题