Public 没有添加的列表
Public List without Add
public class RegistrationManager
{
public List<object> RegisteredObjects;
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
我希望 RegisterObjects
可以从 class 外部访问,但填充 RegisterObjects
列表的唯一方法是通过 TryRegisterObject(object o)
。
这可能吗?
将其隐藏在 IEnumerable 下。您将在 class 之外获得 "readonly" 集合,并在其中使用 List:
public class RegistrationManager
{
public IEnumerable<object> RegisteredObjects
{
get
{
return _registeredObjects;
}
}
private List<object> _registeredObjects;
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
使用 IReadOnlyCollection 的更多受保护变体:
public class RegistrationManager
{
public IReadOnlyCollection<object> RegisteredObjects
{
get { return new ReadOnlyCollection<object>(_registeredObjects); }
}
private List<object> _registeredObjects;
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
类似的东西(我猜你必须有 只读访问权限 这意味着 Add
、RemoveAt
、Clear
等. 不允许):
public class RegistrationManager
{
// change RegisteredObjects to be private
//TODO: do you really want List<object> instead of, say, List<RegisteredItem>?
private List<object> RegisteredObjects = new List<object>();
// let RegisteredObjects be visible as read-only
public IReadOnlyList<object> Items {
get {
return RegisteredObjects;
}
}
// your TryRegisterObject
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
该解决方案的缺点是,从技术上讲,可以回退,例如
RegistrationManager manager = ...
// you can't do this
// manager.Items.Add(new Object()); // <- compile time error
// but can do this
((List<Object>) (manager.Items)).Add(new Object());
return 作为 IReadOnlyList
public class RegistrationManager
{
private List<object> _registeredObjects;
public IReadOnlyList<object> RegisteredObjects
{
get{ return _registeredObjects; }
}
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
是的,您可以公开 IReadOnlyCollection(T) 类型的 属性。
List(T) implements IReadonlyCollection(T)
public class RegistrationManager
{
private List<object> _registeredObjects;
public IReadOnlyCollection<object> RegisteredObjects
{
get
{
return _registeredObjects as IReadOnlyCollection<object>;
}
}
}
我会把它隐藏在 ReadonlyCollection 下。在这种情况下,客户端将无法通过转换为 IList 来添加元素。这完全取决于您想要的安全程度(在最简单的情况下,公开 IEnumerable 就足够了)。
public class RegistrationManager
{
private List<object> _registeredObjects;
ReadOnlyCollection<object> _readOnlyRegisteredObjects;
public RegistrationManager()
{
_registeredObjects=new List<object>();
_readOnlyRegisteredObjects=new ReadOnlyCollection<object>(_registeredObjects);
}
public IEnumerable<object> RegisteredObjects
{
get { return _readOnlyRegisteredObjects; }
}
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
public class RegistrationManager
{
public List<object> RegisteredObjects;
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
我希望 RegisterObjects
可以从 class 外部访问,但填充 RegisterObjects
列表的唯一方法是通过 TryRegisterObject(object o)
。
这可能吗?
将其隐藏在 IEnumerable 下。您将在 class 之外获得 "readonly" 集合,并在其中使用 List:
public class RegistrationManager
{
public IEnumerable<object> RegisteredObjects
{
get
{
return _registeredObjects;
}
}
private List<object> _registeredObjects;
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
使用 IReadOnlyCollection 的更多受保护变体:
public class RegistrationManager
{
public IReadOnlyCollection<object> RegisteredObjects
{
get { return new ReadOnlyCollection<object>(_registeredObjects); }
}
private List<object> _registeredObjects;
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
类似的东西(我猜你必须有 只读访问权限 这意味着 Add
、RemoveAt
、Clear
等. 不允许):
public class RegistrationManager
{
// change RegisteredObjects to be private
//TODO: do you really want List<object> instead of, say, List<RegisteredItem>?
private List<object> RegisteredObjects = new List<object>();
// let RegisteredObjects be visible as read-only
public IReadOnlyList<object> Items {
get {
return RegisteredObjects;
}
}
// your TryRegisterObject
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
该解决方案的缺点是,从技术上讲,可以回退,例如
RegistrationManager manager = ...
// you can't do this
// manager.Items.Add(new Object()); // <- compile time error
// but can do this
((List<Object>) (manager.Items)).Add(new Object());
return 作为 IReadOnlyList
public class RegistrationManager
{
private List<object> _registeredObjects;
public IReadOnlyList<object> RegisteredObjects
{
get{ return _registeredObjects; }
}
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
是的,您可以公开 IReadOnlyCollection(T) 类型的 属性。
List(T) implements IReadonlyCollection(T)
public class RegistrationManager
{
private List<object> _registeredObjects;
public IReadOnlyCollection<object> RegisteredObjects
{
get
{
return _registeredObjects as IReadOnlyCollection<object>;
}
}
}
我会把它隐藏在 ReadonlyCollection 下。在这种情况下,客户端将无法通过转换为 IList 来添加元素。这完全取决于您想要的安全程度(在最简单的情况下,公开 IEnumerable 就足够了)。
public class RegistrationManager
{
private List<object> _registeredObjects;
ReadOnlyCollection<object> _readOnlyRegisteredObjects;
public RegistrationManager()
{
_registeredObjects=new List<object>();
_readOnlyRegisteredObjects=new ReadOnlyCollection<object>(_registeredObjects);
}
public IEnumerable<object> RegisteredObjects
{
get { return _readOnlyRegisteredObjects; }
}
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}