I have the following class:
class SampleClass
{
private ArrayList mMyList;
SampleClass()
{
// Initialize mMyList
}
public ArrayList MyLis
Just to expand on JaredPar's answer. As he said, returning the actual backing field is not completely safe, since the user is still able to dynamically cast the IEnumerable
back to an ArrayList
.
I would write something like this to be sure no modifications to the original list are made:
public IEnumerable MyList
{
get
{
foreach (object o in mMyList)
yield return o;
}
}
Then again, I would probabily also use a generic list (IEnumerable
) to be completely type safe.