Properly exposing a List?

前端 未结 8 1459
谎友^
谎友^ 2021-02-02 01:20

I know I shouldn\'t be exposing a List in a property, but I wonder what the proper way to do it is? For example, doing this:

public static          


        
相关标签:
8条回答
  • 2021-02-02 01:28

    Yes and No. Yes, there is a performance overhead, because a new object is created. No, your list is not cloned, it is wrapped by the ReadOnlyCollection.

    0 讨论(0)
  • 2021-02-02 01:28

    AsEnumerable and ReadOnlyCollection have problem when your enumeration is at midway and collection gets modified. These things are not thread safe. Returning them as an array and caching them at time of calling can be much better option.

    For example,

    public static String[] List{
       get{
          return _List.ToArray();
       }
    } 
    
    //While using ...
    
    String[] values = Class1.List;
    
    foreach(string v in values){
      ...
    }
    
    // instead of calling foreach(string v in Class1.List)
    // again and again, values in this context will not be
    // duplicated, however values are cached instance so 
    // immediate changes will not be available, but its
    // thread safe
    foreach(string v in values){
      ...
    }
    
    0 讨论(0)
  • 2021-02-02 01:35

    Use AsReadOnly() - see MSDN for details

    0 讨论(0)
  • 2021-02-02 01:39

    If you expose your list as IEnumerable, I wouldn't worry about callers casting back to List. You've explicitly indicated in the contract of your class that only the operations defined in IEnumerable are allowed on this list. So you have implicitly stated that the implementation of that list could change to pretty much anything that implements IEnumerable.

    0 讨论(0)
  • 2021-02-02 01:45

    Exposing a List<T> as a property isn't actually the root of all evil; especially if it allows expected usage such as foo.Items.Add(...).

    You could write a cast-safe alternative to AsEnumerable():

    public static IEnumerable<T> AsSafeEnumerable<T>(this IEnumerable<T> data) {
        foreach(T item in data) yield return item;
    }
    

    But your biggest problem at the moment is thread safety. As a static member, you might have big problems here, especially if it is in something like ASP.NET. Even ReadOnlyCollection over an existing list would suffer from this:

            List<int> ints = new List<int> { 1, 2, 3 };
            var ro = ints.AsReadOnly();
            Console.WriteLine(ro.Count); // 3
            ints.Add(4);
            Console.WriteLine(ro.Count); // 4
    

    So simply wrapping with AsReadOnly is not enough to make your object thread-safe; it merely protects against the consumer adding data (but they could still be enumerating it while your other thread adds data, unless you synchronize or make copies).

    0 讨论(0)
  • 2021-02-02 01:47

    If the class has no other purpose you could inherit from list and override the add method and have it throw an exception.

    0 讨论(0)
提交回复
热议问题