我有一个静态类和一个getter来返回我的List集合。现在我已经阅读并被告知要返回ICollection而不是List。使用public static ICollection优于public static List的优势是什么?
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);
}
}
}
}
Collection
和List
具体类之间的区别。我认为这个问题不是重复的。 - Richard Szalay