J3soon
J3soon

Reputation: 3143

C# Methods with infinite parameters array access

After learning how to Create a method with infinite parameters, I wonder is it legal to store the parameter array into a array. Will it cause any problem, since I don't see many people use this approach.

Code below :

class Foo
{
    private String[] Strings;

    public Foo(params String[] strings)
    {
        Strings = strings;
    }
    ...
}

Upvotes: 1

Views: 541

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500555

That's fine - it's just an array.

All the compiler does with a parameter array is convert a call like this:

Foo("x", "y");

into:

Foo(new string[] { "x", "y" });

That's really all there is to it. Anything you'd expect to be appropriate with the second call is fine with a parameter array.

Arrays passed into public methods are rarely suitable to store directly due to all arrays being mutable - but that's a matter of how you handle mutable parameter types rather than being specific to parameter arrays.

Upvotes: 4

Related Questions