【发布时间】:2012-09-06 13:11:43
【问题描述】:
我有一个带有私有静态 List<String> 集合的类。现在我想返回一个只读列表。这会是理想的方式吗?你会用另一种方式吗?这是正确的方法吗?
namespace Test
{
static class Storage
{
private static List<string> store;
static Storage()
{
store = new List<string>();
}
//Is it okay to have a getter in my static class to return my List Collection
public static System.Collections.ObjectModel.ReadOnlyCollection<string>getList
{
get
{
return stores.AsReadOnly();
}
}
public static void addString(string add)
{
store.Add(add);
}
}
}
【问题讨论】:
-
除了很多不必要的换行符,我看不出你的代码有什么问题。你在担心什么?
-
@DStanley - 你可以编辑掉那些,你知道吗?
-
我想知道我是否实现了这个权利,其次,如果使用 ReadOnlyCollection 是返回只读列表集合的理想方式。
-
@Oded - 是的,我知道 - 只是发泄个人的烦恼 :)