5
public class SomeClass
{
    public SomeClass(SomeType[] elements)
    {
        
    }
    public SomeClass(params SomeType[] elements)
    {
        
    }
}

I printed this code and got CS0111 error. I'm surprised, are SomeType[] elements and params SomeType[] elements the same arguments?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • 4
    Both constructors have the same signature. `params` keyword does not change method signature. – Rafael Sep 19 '22 at 14:01
  • 3
    `params` is not the part of the signature of the function. – Eldar Sep 19 '22 at 14:02
  • 1
    https://stackoverflow.com/questions/27375707/what-is-the-difference-between-params-and-array-parameter-and-when-should-it – Rand Random Sep 19 '22 at 14:02
  • 1
    Note that `params` is just a metadata keyword for the C# compiler and has no bearing on the method signature nor on the compilation of the method itself. Basically, `params` is just an instruction for the compiler regarding **call sites** (i.e. callers) of this method to translate a method call in source code like `SomeFooWithParams(1, 2, 3, 4)` into `SomeFooWithParams(new[] { 1, 2, 3, 4 })` (for simplicity, lets ignore right now any other non-params parameters a method might declare...) –  Sep 19 '22 at 14:08

2 Answers2

5

Yes, params does not make the parameter into a different type. Both translate to an array of SomeType.

Simmetric
  • 1,443
  • 2
  • 12
  • 20
2

The difference between the two is that one requires a single concrete array, while the other also allows it to be called with individual items such as

SomeClass("Alpha", "Omega", "Gamma")

Choose the params one if you need to call it as above and delete the other.

Why?

The error speaks to the fact that both are, to the compiler, the same signature SomeType[] hence the error.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122