2014-03-29 26 views
0

我在我的C#類中有以下方法。通過列表<string>或字符串[]數組到C#方法

對於名爲'collections'的參數我希望具有傳遞List或string []數組的靈活性。 我知道我可以簡單地將參數類型設置爲對象類型,但是否還有其他更高效的類型可以用來執行此操作?

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

回答

6

你可以使用IList<string>

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

Why array implements IList?

+0

所以我可以只通過像ProcessDoc(新的String [] { 「X」, 「Y」, 「Z」 的數組},「太陽」)? – Sunil

+0

@Sunil:是的,你可以。 –

+0

好的。所以Array和List實現了IList接口,這就是我們在這種情況下可以使用IList的原因。謝謝。 – Sunil

相關問題