c# - 使 SortedList 只读

标签 c# dictionary sortedlist

我经常有类将列表公开为 ReadOnlyCollection<T> s,即

public class Class
{
  List<string> list;

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

对于 IDictionary<T,U> 执行此操作的最佳方法是什么?例如 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();
    }
}

[编辑] @Simon Buchan 和@Cory Nelson 指出,对于那些不支持的方法,最好使用隐式接口(interface)实现。相应地更新了代码。

关于c# - 使 SortedList 只读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6066643/

相关文章:

c# - Entity Framework 向所有查询添加 where 子句

string - 如何在 Golang 中将映射解压缩为字符串格式的关键字参数?

Android SortedList 删除方法正在跳过项目

c# - 何时使用 SortedList<TKey, TValue> 而不是 SortedDictionary<TKey, TValue>?

c# - 上传图片返回空文件

c# - AngularJS 和 Bootstrap 在 IIS 7.5 中部署后无法正确呈现

arrays - 修改字典数组内部的字典属性。错误: Cannot assign to immutable expression of type [String:AnyObject]

python - 将嵌套列表的字典转换为 pandas DataFrame

mongodb - Redis/Mongo 排序列表的复杂度

c# - 如何将 JSON 反序列化到这个 POCO 类中?