I know there's a proposal for params IEnumerable<T>.

When designing an API, when should we choose params T[] (params array) over IEnumerable<T>. Or should both be implemented for better end-user experience?

Example:

void MyCoolMethod(params string[] strings)

void MyCoolMethod(IEnumerable<string> strings)

When using params invocation is greatly simplified, but then if you want to pass some IEnumerable then you have to call ToArray.

The params keyword is only useful, when you as the coder will pass a number of strings that can vary between calls and you don't want to put them into an array manually for every call.

The IEnumerable<T> data type is useful, when you as a coder have a collection of any type and want to pass it.

So tailor your method to your audience. Those string you pass, do they come as hardcoded string from the developer? Like .Allow("GET", "PUT", "POST") or do they come from a different routine in your program like .Allow(this.ReadAllowedVerbsFromConfiguration())?

When the human sitting in front of the machine has to type out the parameters, use params, otherwise an IEnumerable<T> might be the better choice.

  • I think both situations are possible. – Konrad Dec 7 at 9:34
  • 1
    @Konrad then have an overload that passes one to the other – Caleth Dec 7 at 9:50
  • How the input is to be expressed will determine how easy it is to use this function. So find out what makes for easy usage. Implement function signatures that are sensible and support that usage. – Kain0_0 Dec 7 at 10:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.