I have the following class:
class SampleClass
{
private ArrayList mMyList;
SampleClass()
{
// Initialize mMyList
}
public ArrayList MyLis
Use a ReadOnlyCollection
.
return new ReadOnlyCollection
.
You can also make the ReadOnlyCollection
a field, and it will automatically reflect changes in the underlying list. This is the most efficient solution.
If you know that mMyList only contains one type of object (eg, it has nothing but DateTimes),
you can return a ReadOnlyCollection
(or some other type) for additional type safety. If you're using C# 3, you can simply write
return new ReadOnlyCollection
(mMyList.OfType ().ToArray())
However, this will not automatically update with the underlying list, and is also less efficient (It will copy the entire list). The best option is to make mMyList a generic List
(eg, a List
)