“Read only” Property Accessor in C#

前端 未结 8 1599
遇见更好的自我
遇见更好的自我 2021-02-12 14:08

I have the following class:

class SampleClass
{
   private ArrayList mMyList;

   SampleClass()
   {
       // Initialize mMyList
   }

   public ArrayList MyLis         


        
相关标签:
8条回答
  • 2021-02-12 14:45

    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>)

    0 讨论(0)
  • 2021-02-12 14:47

    Available from .NET v2.0

        public ArrayList MyList { get; private set; }
    
    0 讨论(0)
  • 2021-02-12 14:49

    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.

    0 讨论(0)
  • 2021-02-12 14:56

    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.

    0 讨论(0)
  • 2021-02-12 14:57

    List(T).AsReadOnly Method =)

    0 讨论(0)
  • 2021-02-12 15:02

    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();
    }
    
    0 讨论(0)
提交回复
热议问题