By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

How do you stop a user from removing elements in a list<>?

Aimee Bailey
Expert 100+
P: 197
In a library I am writing, I wish to stop people from removing item's from an List<> collection, is there an alternative or a way of overloading the Remove method that would stop other developers removing elements?

Aimee.
Oct 18 '10 #1

✓ answered by Christian Binder

Have you ever tried using List.AsReadOnly()-method?

This won't allow Add, Remove, Clear, ...

Share this Question
Share on Google+
5 Replies


Expert 100+
P: 218
Have you ever tried using List.AsReadOnly()-method?

This won't allow Add, Remove, Clear, ...
Oct 18 '10 #2

Aimee Bailey
Expert 100+
P: 197
perfect :) just used that in the property and it does the job, thanks!

Aimee.
Oct 18 '10 #3

GaryTexmo
Expert 100+
P: 1,501
Just to throw alternatives around, you can also create your own class to either wrap, or inherit from a List<>. If the former, you can provide your own functionality to expose only the access methods you wish. If the latter, I think you can override the Remove method and have it do nothing, or throw an exception so the programmer knows that method isn't allowed.
Oct 18 '10 #4

Aimee Bailey
Expert 100+
P: 197
I actually considered that, allthough it meant overriding the Add, Remove and Insert routines.
Oct 18 '10 #5

Curtis Rutland
Expert 2.5K+
P: 3,256
If you need a collection that you can edit but not delete from, here's something I've used in the past:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5.  
  6. namespace Example
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             ReadOnlyList<string> list = new ReadOnlyList<string>();
  13.             list.Add("something");
  14.             list.Add("else");
  15.             list.Add("test");
  16.             foreach (string s in list)
  17.                 Console.WriteLine(s);
  18.             var sub = list.Where(x => x.Contains("t"));
  19.             foreach (string s in sub)
  20.                 Console.WriteLine(s);
  21.             Console.ReadKey();
  22.         }
  23.     }
  24.  
  25.     public class ReadOnlyList<T> : IEnumerable<T>
  26.     {
  27.         private List<T> list;
  28.  
  29.         public ReadOnlyList()
  30.         {
  31.             list = new List<T>();
  32.         }
  33.  
  34.         public ReadOnlyList(IEnumerable<T> collection)
  35.         {
  36.             list = new List<T>(collection);
  37.         }
  38.  
  39.         public void Add(T item)
  40.         {
  41.             list.Add(item);
  42.         }
  43.  
  44.         public void AddRange(IEnumerable<T> collection)
  45.         {
  46.             list.AddRange(collection);
  47.         }
  48.  
  49.         public void Insert(int index, T item)
  50.         {
  51.             list.Insert(index, item);
  52.         }
  53.  
  54.         public void InsertRange(int index, IEnumerable<T> collection)
  55.         {
  56.             list.InsertRange(index, collection);
  57.         }
  58.  
  59.         public T this[int i]
  60.         {
  61.             get { return list[i]; }
  62.             set { list[i] = value; }
  63.         }
  64.  
  65.         public int Count { get { return list.Count; } }
  66.  
  67.         #region IEnumerable Members
  68.         public IEnumerator GetEnumerator()
  69.         {
  70.             return list.GetEnumerator();
  71.         }
  72.         #endregion
  73.  
  74.         #region IEnumerable<T> Members
  75.         IEnumerator<T> IEnumerable<T>.GetEnumerator()
  76.         {
  77.             return list.GetEnumerator();
  78.         }
  79.         #endregion
  80.     }
  81.  
  82.     public static class ExtensionMethods
  83.     {
  84.         public static ReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> collection)
  85.         {
  86.             return new ReadOnlyList<T>(collection);
  87.         }
  88.     }
  89. }
The basic idea is to implement IEnumerable<T>. That way, you get all the LINQ extension methods, and you can easily do foreach loops. Also, I've added an indexer, so you can treat it like an array if you want.

You can add/remove methods as needed, but you can see the pattern here.

It's a little different than inheriting from List, because with list, you still have the methods the base class has. Then, you'd have to throw exceptions or (worse) do nothing when those undesirable methods are called. In this way, you can just treat it like any other IEnumerable. System.Linq even includes a ToList extension method.

If you just wanted to be able to add, but not insert, or whatever, you could remove those methods. But if the AsReadOnly method works for you, that's a great solution too.

Also tacked on an extension method to turn other IEnumerables into a ReadOnlyList.
Oct 18 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.