wp7 BaseDictionary<TKey, TValue>
/// 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;
}
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>的更多相关文章
- .net源码分析 – Dictionary<TKey, TValue>
接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blo ...
- .net源码分析 - ConcurrentDictionary<TKey, TValue>
List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...
- C# KeyValuePair<TKey,TValue>的用法-转载
C# KeyValuePair<TKey,TValue>的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过 它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和 ...
- .NET中Dictionary<TKey, TValue>浅析
.NET中Dictionary<TKey, Tvalue>是非常常用的key-value的数据结构,也就是其实就是传说中的哈希表..NET中还有一个叫做Hashtable的类型,两个类型都 ...
- .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>
无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...
- IDictionary<TKey, TValue> vs. IDictionary
Enumerating directly over an IDictionary<TKey,TValue>returns a sequence of KeyValuePair struc ...
- Dictionary<TKey, TValue> 类
C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及 ...
- 线程安全集合 ConcurrentDictionary<TKey, TValue> 类
ConcurrentDictionary<TKey, TValue> 类 [表示可由多个线程同时访问的键/值对的线程安全集合.] 支持 .NET Framework 4.0 及以上. 示例 ...
- C# 字典 Dictionary<Tkey,Tvalue>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...
随机推荐
- 利用a标签自动解析URL
parseURL // This function creates a new anchor element and uses location // properties (inherent) to ...
- 创建gbk编码
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_1803 ...
- CentOS 设置网络(修改IP&修改网关&修改DNS)--update.14.08.15
自己电脑上装的虚拟机用桥接方式连接物理机,虚拟机重启后ip会发生变化,非常阻碍Xshell的连接和hosts指定的dns. 通过修改IP为static模式,保持IP不变. ============== ...
- 连接oracle读取数据
没怎么用过oracle,而且是在地税内网内部估计是防火墙的原因虚拟机里也连不上oracle,刚开始费了很多周折查找问题,现在又放弃使用直连数据库了,记下来以备后用吧 public class Load ...
- 数字、大写字母、小写字母,谁的ASCII码最大?
常见ASCII码的大小规则:0~9<A~Z<a~z几个常见字母的ASCII码大小: “A”为65:“a”为97:“0”为 48.
- win7下搭建PHP环境
一.安装软件 1.apache下载地址:http://httpd.apache.org/download.cgi 2.php下载地址:http://windows.php.net/download/ ...
- JS的运行机制
代码块: JS中的代码块是指由<script>标签分割的代码段.JS是按照代码块来进行编译和执行的,代码块间相互独立(即就算代码块1出错,但不影响代码块2的加载和执行),但变量和方法共享. ...
- sql 去除重复记录
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from ...
- 繁华模拟赛 ljw搭积木
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 诠释Linux中『一切都是文件』概念和相应的文件类型
导读 在 Unix 和它衍生的比如 Linux 系统中,一切都可以看做文件.虽然它仅仅只是一个泛泛的概念,但这是事实.如果有不是文件的,那它一定是正运行的进程. 要理解这点,可以举个例子,您的根目录( ...