/// <summary>
/// Represents a dictionary mapping keys to values.
/// </summary>
/// 
/// <remarks>
/// Provides the plumbing for the portions of IDictionary<TKey,
/// TValue> which can reasonably be implemented without any
/// dependency on the underlying representation of the dictionary.
/// </remarks>
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(PREFIX + "DictionaryDebugView`2" + SUFFIX)]
public abstract class BaseDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
    private const string PREFIX = "System.Collections.Generic.Mscorlib_";
    private const string SUFFIX =",mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089";

private KeyCollection keys;
    private ValueCollection values;

protected BaseDictionary() { }

public abstract int Count { get; }
    public abstract void Clear();
    public abstract void Add(TKey key, TValue value);
    public abstract bool ContainsKey(TKey key);
    public abstract bool Remove(TKey key);
    public abstract bool TryGetValue(TKey key, out TValue value);
    public abstract IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator();
    protected abstract void SetValue(TKey key, TValue value);

public bool IsReadOnly {
        get { return false; }
    }

public ICollection<TKey> Keys {
        get {
            if (this.keys == null)
                this.keys = new KeyCollection(this);

return this.keys;
        }
    }

public ICollection<TValue> Values {
        get {
            if (this.values == null)
                this.values = new ValueCollection(this);

return this.values;
        }
    }

public TValue this[TKey key] {
        get {
            TValue value;
            if (!this.TryGetValue(key, out value))
                throw new KeyNotFoundException();

return value;
        }
        set {
            SetValue(key, value);
        }
    }

public void Add(KeyValuePair<TKey, TValue> item) {
        this.Add(item.Key, item.Value);
    }

public bool Contains(KeyValuePair<TKey, TValue> item) {
        TValue value;
        if (!this.TryGetValue(item.Key, out value))
            return false;

return EqualityComparer<TValue>.Default.Equals(value, item.Value);
    }

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

public bool Remove(KeyValuePair<TKey, TValue> item) {
        if (!this.Contains(item))
            return false;

        return this.Remove(item.Key);
    }

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
        return this.GetEnumerator();
    }

private abstract class Collection<T> : ICollection<T> {
        protected readonly IDictionary<TKey, TValue> dictionary;

protected Collection(IDictionary<TKey, TValue> dictionary) {
            this.dictionary = dictionary;
        }

public int Count {
            get { return this.dictionary.Count; }
        }

public bool IsReadOnly {
            get { return true; }
        }

public void CopyTo(T[] array, int arrayIndex) {
            Copy(this, array, arrayIndex);
        }

public virtual bool Contains(T item) {
            foreach (T element in this)
                if (EqualityComparer<T>.Default.Equals(element, item))
                    return true;
            return false;
        }

public IEnumerator<T> GetEnumerator() {
            foreach (KeyValuePair<TKey, TValue> pair in this.dictionary)
                yield return GetItem(pair);
        }

protected abstract T GetItem(KeyValuePair<TKey, TValue> pair);

public bool Remove(T item) {
            throw new NotSupportedException("Collection is read-only.");
        }

public void Add(T item) {
            throw new NotSupportedException("Collection is read-only.");
        }

public void Clear() {
            throw new NotSupportedException("Collection is read-only.");
        }

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }
    }

[DebuggerDisplay("Count = {Count}")]
    [DebuggerTypeProxy(PREFIX + "DictionaryKeyCollectionDebugView`2" + SUFFIX)]
    private class KeyCollection : Collection<TKey> {
        public KeyCollection(IDictionary<TKey, TValue> dictionary)
            : base(dictionary) { }

protected override TKey GetItem(KeyValuePair<TKey, TValue> pair) {
            return pair.Key;
        }
        public override bool Contains(TKey item) {
            return this.dictionary.ContainsKey(item);
        }
    }

[DebuggerDisplay("Count = {Count}")]
    [DebuggerTypeProxy(PREFIX + "DictionaryValueCollectionDebugView`2" + SUFFIX)]
    private class ValueCollection : Collection<TValue> {
        public ValueCollection(IDictionary<TKey, TValue> dictionary)
            : base(dictionary) { }

protected override TValue GetItem(KeyValuePair<TKey, TValue> pair) {
            return pair.Value;
        }
    }

private static void Copy<T>(ICollection<T> source, T[] array, int arrayIndex) {
        if (array == null)
            throw new ArgumentNullException("array");

if (arrayIndex < 0 || arrayIndex > array.Length)
            throw new ArgumentOutOfRangeException("arrayIndex");

if ((array.Length - arrayIndex) < source.Count)
            throw new ArgumentException("Destination array is not large enough. Check array.Length and arrayIndex.");

foreach (T item in source)
            array[arrayIndex++] = item;
    }
}

wp7 BaseDictionary<TKey, TValue>的更多相关文章

  1. .net源码分析 – Dictionary<TKey, TValue>

    接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blo ...

  2. .net源码分析 - ConcurrentDictionary<TKey, TValue>

    List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...

  3. C# KeyValuePair<TKey,TValue>的用法-转载

    C# KeyValuePair<TKey,TValue>的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过 它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和 ...

  4. .NET中Dictionary<TKey, TValue>浅析

    .NET中Dictionary<TKey, Tvalue>是非常常用的key-value的数据结构,也就是其实就是传说中的哈希表..NET中还有一个叫做Hashtable的类型,两个类型都 ...

  5. .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>

    无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...

  6. IDictionary<TKey, TValue> vs. IDictionary

    Enumerating directly over an IDictionary<TKey,TValue>returns a sequence of  KeyValuePair struc ...

  7. Dictionary<TKey, TValue> 类

    C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及 ...

  8. 线程安全集合 ConcurrentDictionary<TKey, TValue> 类

    ConcurrentDictionary<TKey, TValue> 类 [表示可由多个线程同时访问的键/值对的线程安全集合.] 支持 .NET Framework 4.0 及以上. 示例 ...

  9. C# 字典 Dictionary<Tkey,Tvalue>

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...

随机推荐

  1. Centos 5.x/6.x 配置163网易yum源

    Centos系统默认都是系统自带的yum源,国内用户用yum源安装比较慢,为了提高效率,一般我们会配置国内的yum源.国内比较好的yum源有网易yum源.搜狐yum源等. 我感觉网易的yum源比较好用 ...

  2. 使用key链接远程Git仓库

    使用密钥来访问Git仓库比密码安全多了,只要把公钥配置在远程端,把密钥放到~/.ssh/里以id_rsa命名即可. 不过有人要问如果有多个仓库,而且用了不同密钥怎么办? 这时候可以在~/.ssh/文件 ...

  3. mysql delete删除记录数据库空间不减少问题解决方法

    记得在中学时学计算机时老师就告诉我delete删除记录只是给数据库中的记录加一个删除标识了,这样数据库空间并不是减少了,当时没想这么多,昨天发现一个数据库利用delete 删除之后容量没变,后来百度了 ...

  4. Visual Studio Online Integrations-Build and release

                                      原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-dire ...

  5. hibernate criteria中Restrictions的用法

    方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge ...

  6. 构建seajs业务模块之grunt VS spm build

    在最开始,我并不知道grunt可以构建CMD模块.(以下spm指代spm build) 当时正困惑于如何用spm方便的构建业务模块,后来看到@twinstony (感谢@twinstony的分享)使用 ...

  7. [整理]iOS开发学习

    最近想趁着休假,花点时间了解下最新的iOS8下的新特性以及Swift语言(想大致了解下和Objective-C有了哪些改进和不同) 可以通过Chris Lattner:Swift 编程语言首席架构师初 ...

  8. XML做下拉列表

    5-18X.php主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  9. Y2K Accounting Bug(贪心)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10945   Accepted: 54 ...

  10. DLL注入之SetWindowsHookEx

    注:本文章转载自网络 函数功能:该函数将一个应用程序定义的挂钩处理过程安装到挂钩链中去,您可以通过安装挂钩处理过程来对系统的某些类型事件进行监控,这些事件与某个特定的线程或系统中的所有事件相关. 函数 ...