“Read only” Property Accessor in C#

你说的曾经没有我的故事 提交于 2019-12-03 09:54:55

With an ArrayList you are fairly limited because there is no readonly non-generic collection class in the BCL. The quick and dirty solution is to return a type of IEnumerable.

   public IEnumerable MyList
   {
       get { return mMyList;}
   }

This won't actually prevent someone from casting to ArrayList but it won't allow edits by default either

You can return an effectively readonly list by calling ArrayList.ReadOnly. However it's return type is an ArrayList so the user would still be able to compile with .Add but it would produce a runtime error.

Use the ArrayList.ReadOnly() method to construct and return a read-only wrapper around the list. it won't copy the list, but simply make a read-only wrapper around it. In order to get compile-time checking, you probably want to return the read-only wrapper as IEnumerable as @Jared suggests.

public IEnumerable MyList
{
     get { return ArrayList.ReadOnly(mMyList); }
}

A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection. If changes are made to the underlying collection, the read-only collection reflects those changes.

This method is an O(1) operation.

Reference

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<T>) to be completely type safe.

Use a ReadOnlyCollection.

return new ReadOnlyCollection<object>(mMyList).

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<DateTime> (or some other type) for additional type safety. If you're using C# 3, you can simply write

return new ReadOnlyCollection<DateTime>(mMyList.OfType<DateTime>().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<T> (eg, a List<DateTime>)

You should wrap the return value in a read only collection.

Available from .NET v2.0

    public ArrayList MyList { get; private set; }

Just make the property as Sealed (or NotInheritable in VB.NET) and readonly, like this:

   public sealed readonly ArrayList MyList
   {
       get { return mMyList;}
   }

Also don't forget to make the backing field as readonly:

   private readonly ArrayList mMyList;

But in order to initialize the value of mMyList, you must initialize it ONLY in the constructor, as in this example:

public SampleClass()
{
   // Initialize mMyList
   mMyList = new ArrayList();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!