【发布时间】:2012-09-03 02:18:08
【问题描述】:
可能重复:
What is the difference between List (of T) and Collection(of T)?
我有一个静态类和一个 getter 来返回我的列表集合。现在我阅读并被告知要返回 ICollection 而不是 List。与 public static List 相比,使用 public static ICollection 有什么优势?
static class Storage
{
private static List<string> store;
static Storage()
{
store = new List<string>();
}
public static ICollection<string> getList
{
get
{
return store.AsReadOnly();
}
}
public static void addString(string add)
{
store.Add(add);
}
public static void removeString(string remove)
{
store.Remove(remove);
}
public static void display()
{
foreach (String view in store)
{
Console.WriteLine(view);
}
}
}
}
【问题讨论】:
-
@Gary.S - 链接的问题是指
Collection andList` 具体类之间的区别。我不认为这个问题是重复的。
标签: c# icollection