开发者

Making a SortedList readonly

开发者 https://www.devze.com 2023-03-07 16:40 出处:网络
I often have classes exposing lists as 开发者_如何学CReadOnlyCollection<T>s, i.e. public class Class

I often have classes exposing lists as 开发者_如何学CReadOnlyCollection<T>s, i.e.

public class Class
{
  List<string> list;

  public ReadOnlyCollection<string> TheList
  {
    get { return list.AsReadOnly(); }
  }
}

What's the best way to do this for an IDictionary<T,U> such as a SortedList<string, string>?


public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
    private readonly IDictionary<TKey, TValue> sourceDictionary;

    public ICollection<TKey> Keys
    {
        get { return sourceDictionary.Keys; }
    }

    public ICollection<TValue> Values
    {
        get { return sourceDictionary.Values; }
    }

    public TValue this[TKey key]
    {
        get { return sourceDictionary[key]; }
        set { throw new NotSupportedException(); }
    }

    public int Count
    {
        get { return sourceDictionary.Count; }
    }

    public bool IsReadOnly
    {
        get { return true; }
    }

    public ReadOnlyDictionary(IDictionary<TKey, TValue> sourceDictionary)
    {
        AssertUtilities.ArgumentNotNull(sourceDictionary, "sourceDictionary");

        this.sourceDictionary = sourceDictionary;
    }

    void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
    {
        throw new NotSupportedException();
    }

    public bool ContainsKey(TKey key)
    {
        return sourceDictionary.ContainsKey(key);
    }

    bool IDictionary<TKey, TValue>.Remove(TKey key)
    {
        throw new NotSupportedException();
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
        return sourceDictionary.TryGetValue(key, out value);
    }

    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
    {
        throw new NotSupportedException();
    }

    void ICollection<KeyValuePair<TKey, TValue>>.Clear()
    {
        throw new NotSupportedException();
    }

    public bool Contains(KeyValuePair<TKey, TValue> item)
    {
        return sourceDictionary.Contains(item);
    }

    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    {
        sourceDictionary.CopyTo(array, arrayIndex);
    }

    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
    {
        throw new NotSupportedException();
    }

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
        return sourceDictionary.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable)sourceDictionary).GetEnumerator();
    }
}

[Edit] @Simon Buchan and @Cory Nelson pointed out that it is better to use implicit interface implementation for those methods that are not supported. Updated the code accordingly.


Create a ReadOnlyDictionary class that implements IDictionary as a wrapper around an internal Dictionary instance. For the methods that would modify the dictionary, throw an exception. Implement IsReadOnly to return true. Implement all other methods to pass through to the internal Dictionary instance.


You can do it with standard LINQ methods.

  1. Create you source list:

     List<String> myList = new List<String>() { "A", "B", "C" };
    
  2. Project your list into a dictionary using .ToDictionary linq extension method:

     var myDictionary = myList.ToDictionary(listItem => listItem);
    

Note: The lambda expression peeks a key from you list (rembember that dictionary can only contain unique keys; otherwise consider the use of ILookup which represents a dictionary of list).

  1. Reform your dictionary to a SortedDictionary instance:

     var mySortedDictionary = new SortedDictionary<string, string>(myDictionary);
    
  2. Expose your sorted dictionary as an IReadOnlyDictionary interface as follows:

     public IReadOnlyDictionary MemberDictionary { get; private set; );
    
     // ...somewhere in your constructor or class's initialization method...
     this.MemberDictionary = mySortedDictionary;
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号