[英]Block the List.Add() method
I have a class that contains a private List. 我有一个包含私人列表的类。 I have created a getter and a method to add elements to the list:
我创建了一个getter和一个向列表中添加元素的方法:
public class Test{
private List<T> myList;
public List<T> MyList
{
get { return myList; }
}
public Test()
{
myList = new List<T>();
}
public void AddElements(T element)
{
// Do dome stuff, subscribe to an event of T
myList.Add(element);
}
}
Since everytime an element is added to my list I want to do more things, I do not want that in some part of the code someone add an element directly: 因为每次将一个元素添加到我的列表中时我想做更多的事情,我不希望在代码的某些部分有人直接添加元素:
Test test = new Test();
// Wrong
test.MyList.Add(element);
// Right
test.AddElements(element);
I have thought on creating a new class that implements the IList interface and overrides the Add() method, but I was wondering if there is a more simple/elegant way on "block" this Add() method. 我曾想过创建一个实现IList接口的新类并重写Add()方法,但我想知道是否有一个更简单/更优雅的方法来阻止这个Add()方法。
If you are using at least .NET 4.5, return a IReadOnlyList<T>
. 如果您至少使用.NET 4.5,则返回
IReadOnlyList<T>
。
public class Test{
private List<T> myList;
public IReadOnlyList<T> MyList
{
get { return myList; }
}
public Test()
{
myList = new List<T>();
}
public void AddElements(T element)
{
// Do dome stuff, subscribe to an event of T
myList.Add(element);
}
}
In Test class expose the List
as IReadOnlyList<T>
then you wont be allowed to use MyList.Add directly 在Test类中,将
List
公开为IReadOnlyList<T>
然后您将不被允许直接使用MyList.Add
public class Test{
private List<T> myList;
public IReadOnlyList<T> MyList
{
get { return myList; }
}
public Test()
{
myList = new List<T>();
}
public void AddElements(T element)
{
// Do dome stuff, subscribe to an event of T
myList.Add(element);
}
}
Edit updated it IEnumerable to IReadOnlyList. 编辑将IEnumerable更新为IReadOnlyList。 There are benifits in using IReadOnlyList - IEnumerable<T> vs IReadOnlyList<T>
使用IReadOnlyList有好处 - IEnumerable <T> vs IReadOnlyList <T>
I would return as ReadOnlyCollection<T>
我会以
ReadOnlyCollection<T>
形式返回
Use it like: 使用它像:
public class Test<T>
{
private List<T> myList;
public ReadOnlyCollection<T> MyList
{
get { return myList.AsReadOnly(); }
}
public Test()
{
myList = new List<T>();
}
public void AddElements(T element)
{
// Do dome stuff, subscribe to an event of T
myList.Add(element);
}
}
This way you prevent casting... It will Wrap your List<>
with a ReadOnlyCollection class. 这样你就可以防止强制转换...它将使用ReadOnlyCollection类包装你的
List<>
。
Or you could return it as an Array
like: 或者您可以将其作为
Array
返回:
public T[] MyList
{
get { return myList.ToArray(); }
}
The ToArray()
method will create a copy of the list ToArray()
方法将创建列表的副本
When it is Returned as an IReadOnlyList<T>
. 当它作为
IReadOnlyList<T>
返回时。 You could simply cast it back.. 你可以简单地把它扔掉..
Test test = new Test<int>();
test.AddElements(10);
((List<int>)test.MyList).Add(20);
foreach(var i in test.MyList)
Console.WriteLine(i);
If the only reason for your getter is to add elements you won´t need to directly access the list at all, but you can wrap the call to the Add
-method into your own method: 如果你的getter的唯一原因是添加元素,你根本不需要直接访问列表,但是你可以将调用
Add
-method包装到你自己的方法中:
class MyClass<T> {
private List<T> _myList ...
public void AddElements(T element)
{
// Do dome stuff, subscribe to an event of T
_myList.Add(element);
}
}
Exposing a generic list is furthermore avoided by CA1002 CA1002进一步避免了公开列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.