Search code examples
c#params-keyword

Why can't I have two method signatures with the only difference being the "params" keyword for the array parameter?


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?


Solution

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