将List <string>或string []数组传递给C#方法

Sun*_*nil 0 c# collections parameter-passing

我的C#类中有以下方法.

对于名为'collections'的参数,我希望能够灵活地传递List或string []数组.我知道我可以简单地将参数类型设置为对象类型,但是还有其他更高效的类型可以用来做这个吗?

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

Tim*_*ter 6

你可以使用IList<string>:

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

为什么数组实现IList?

  • 如果索引器不是一个要求,那么`IEnumerable <string>`是更具包容性的选择. (4认同)