0

I have the following method in my C# class.

For the parameter called 'collections' I would like to have the flexibility of either passing a List or string[ ] array. I know I could simply set the parameter type as object type, but is there any other more efficient type I could use to do this?

public string ProcessDoc(List<string> collections, string userName)
{
  //code goes here
   string result = null;
   ...
   ...
   return result;
}

1 Answer 1

7

You could use IList<string>:

public string ProcessDoc(IList<string> collections, string userName)
{
  //code goes here
   string result = null;
   ...
   ...
   return result;
}

Why array implements IList?

Sign up to request clarification or add additional context in comments.

5 Comments

So I could just pass an array like ProcessDoc( new string[] {"x", "y","z"}, "sun") ?
Ok. So Array and List implement IList interface, and that is the reason we can use IList in this case. Thanks.
If indexer is not a requirement then IEnumerable<string> is more encompassing alternative.
@Dialecticus, I need the indexer in my C# method.
@Dialecticus: i wanted to give the highest common factor, also IList<string> implements IEnumerable<string>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.