12

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

public static class Class1
{
    private readonly static List<string> _list;

    public static IEnumerable<string> List
    {
        get
        {
            return _list;
            //return _list.AsEnumerable<string>(); behaves the same
        }
    }

    static Class1()
    {
        _list = new List<string>();
        _list.Add("One");
        _list.Add("Two");
        _list.Add("Three");
    }
}

would allow my caller to simply cast back to List<T>:

    private void button1_Click(object sender, EventArgs e)
    {
        var test = Class1.List as List<string>;
        test.Add("Four"); // This really modifies Class1._list, which is bad™
    }

So if I want a really immutable List<T> would I always have to create a new list? For example, this seems to work (test is null after the cast):

    public static IEnumerable<string> List
    {
        get
        {
            return new ReadOnlyCollection<string>(_list);
        }
    }

But I'm worried if there is a performance overhead as my list is cloned every time someone tries to access it?

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • 2
    what's wrong w/exposing a List(of T) as a property? – Jason Aug 25 '09 at 07:37
  • You cannot extend it: http://blogs.msdn.com/fxcop/archive/2006/04/27/faq-why-does-donotexposegenericlists-recommend-that-i-expose-collection-lt-t-gt-instead-of-list-lt-t-gt-david-kean.aspx – Michael Stum Aug 25 '09 at 07:38
  • I don't understand what the point of this is? I mean, I would just expose the property as List... ??? is this a thread-safe issue? – Pure.Krome Aug 25 '09 at 07:44
  • for what do you need to extend it? – Jason Aug 25 '09 at 07:45
  • 1
    The article gives an example: if you ever want notifications when items are added/removed, you've lost because you can't do that with a List, so you have to make a breaking change to your public interface. Also in my case, it was more about getting a Read Only List without much overhead. – Michael Stum Aug 25 '09 at 07:58

8 Answers8

8

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

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • s/exiting/existing/ :P (but +1!) – Ruben Bartelink Aug 25 '09 at 07:41
  • I always forget that yield exists... Thread safety in ASP.net is not that much of an issue as I try not to use mutable static classes in ASP.net because they are shared accross requests. I assume that If I want a "snapshot", there is no way around a copy anyway, so `return new List(_list);` would 'solve' that, and if the caller casts it back to List he only modifies his own copy but not mine. The main point really is to keep the Class1._list 'immutable' to outside callers and expose Add/Remove functions in Class1. – Michael Stum Aug 25 '09 at 07:48
  • 1
    Yes, the full copy protects against all evils except somebody changing properties of **objects** in the list. – Marc Gravell Aug 25 '09 at 08:03
  • 1
    http://stackoverflow.com/questions/1230293/c-how-should-i-use-properties-when-dealing-with-listt-members/1232332#1232332 – Svish Aug 25 '09 at 08:15
4

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.

Henrik
  • 23,186
  • 6
  • 42
  • 92
3

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

Robban
  • 6,729
  • 2
  • 39
  • 47
2

Use AsReadOnly() - see MSDN for details

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • 1
    `AsReadOnly` just calls the `ReadOnlyCollection`'s constructor, in exactly the same way as the example code in the question. – LukeH Aug 25 '09 at 07:37
  • 1
    I'd still use it though. The main point is that it exists for a reason and that the doc for it goes into some detail as to what it does. Having said that, no, my answer aint addding anything that Marc's isnt! – Ruben Bartelink Aug 25 '09 at 07:42
  • 1
    Was torn between this, Henrik's and Marc's answer. Accepting this because I did not know about AsReadOnly(), as it's very concise, as it solves the issue, as I can cache it (creating a private readonly static ReadOnlyCollection _listWrapper;, setting it up in the constructor, and then returning it), and as it seems to be the proper way to do it (I saw Eric Lippert mentioning it as a good way of wrapping it). Thread safety may be an issue, but that's a general issue anyway. – Michael Stum Aug 25 '09 at 08:22
  • I'm honoured. Fair play for the amount of consideration you put into the choice. I'm sure I'm not the only one here with a +1 for Marc's insightful answer though - hopefully the points will ease the pain of his not getting a rub of green! Thanks! – Ruben Bartelink Aug 25 '09 at 09:39
2

You don't need to worry about the overhead of cloning: wrapping a collection with a ReadOnlyCollection does not clone it. It just creates a wrapper; if the underlying collection changes, the readonly version changes also.

If you worry about creating fresh wrappers over and over again, you can cache it in a separate instance variable.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
2

I asked a similar question earlier:

Based on that I would recommend that you use the List<T> internally, and return it as a Collection<T> or IList<T>. Or if it is only necessary to enumerate and not add or antyhing like that, IEnumerable<T>.

On the matter of being able to cast what you return in to other things, I would just say don't bother. If people want to use your code in a way that it was not intended, they will be able to in some way or another. I previously asked a question about this as well, and I would say the only wise thing to do is to expose what you intend, and if people use it in a different way, well, that is their problem :p Some related questions:

Community
  • 1
  • 1
Svish
  • 152,914
  • 173
  • 462
  • 620
1

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.

jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • That's true, and as Svish pointed out it's (near?) impossible to do that anyway without deep-copying the data, but I wanted to have a little barrier to prevent people from shooting themselves in the foot too easily. – Michael Stum Aug 25 '09 at 14:20
0

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){
  ...
}
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • Arrays in public class contract are considered as bad practice – Przemaas Aug 25 '09 at 07:56
  • Not always, but Eric Lippert had some thoughts about that: http://blogs.msdn.com/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx – Michael Stum Aug 25 '09 at 08:06
  • @Przemaas, there is no general rule like arrays are bad practice, its always situation dependent, if you disassemble and look at source of List, it has an array to store its own items as well. When you dont want to modify the contents, and you need read only collection, array is the only fastest way to do it. – Akash Kava Aug 25 '09 at 08:07