博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
增强版字典DictionaryEx
阅读量:6076 次
发布时间:2019-06-20

本文共 6348 字,大约阅读时间需要 21 分钟。

代码

public class DictionaryEx
: IDictionary
{ ///
用户存储数据的字典 /// private IDictionary
_items; ///
默认值 /// private TValue _defaultValue; ///
用于获取值的委托 /// private Converter
_getValue; ///
1返回_defaultValue, 2执行_getValue, 0抛出异常 /// private int _mode = 0; #region 构造函数 ///
初始化 DictionaryEx , key不存在时返回defaultValue /// ///
默认值 public DictionaryEx(TValue defaultValue) { _items = new Dictionary
(); _defaultValue = defaultValue; _mode = 1; } ///
初始化 DictionaryEx , key不存在时返回defaultValue /// ///
默认值 ///
比较键时要使用对象,如果为null则使用默认比较方法 public DictionaryEx(TValue defaultValue, IEqualityComparer
comparer) { _items = new Dictionary
(comparer); _defaultValue = defaultValue; _mode = 1; } ///
初始化 DictionaryEx 只读集合, key不存在时返回defaultValue /// ///
默认值 ///
内部字典 public DictionaryEx(TValue defaultValue, IDictionary
dictionary) { Assertor.AreNull(dictionary, "dictionary"); _items = dictionary; IsReadOnly = true; _defaultValue = defaultValue; _mode = 1; } ///
初始化 DictionaryEx, key不存在时返回defaultValue /// ///
默认值 ///
内部字典 ///
是否只读 public DictionaryEx(TValue defaultValue, IDictionary
dictionary, bool isReadOnly) { Assertor.AreNull(dictionary, "dictionary"); _items = dictionary; IsReadOnly = isReadOnly; _defaultValue = defaultValue; _mode = 1; } ///
初始化 DictionaryEx 设定getValue委托,key不存在时执行委托,并加入集合 /// ///
获取值的委托 public DictionaryEx(Converter
getValue) { Assertor.AreNull(getValue, "getValue"); _items = new Dictionary
(); _getValue = getValue; _mode = 2; } ///
初始化 DictionaryEx 设定getValue委托,key不存在时执行委托,并加入集合 /// ///
获取值的委托 ///
比较键时要使用对象,如果为null则使用默认比较方法 public DictionaryEx(Converter
getValue, IEqualityComparer
comparer) { Assertor.AreNull(getValue, "getValue"); _items = new Dictionary
(comparer); _getValue = getValue; _mode = 2; } ///
初始化 DictionaryEx 设定getValue委托,key不存在时执行委托,并加入集合 /// ///
获取值的委托 ///
集合是否限制外部修改 public DictionaryEx(Converter
getValue, bool isReadOnly) { Assertor.AreNull(getValue, "getValue"); _items = new Dictionary
(); _getValue = getValue; IsReadOnly = isReadOnly; _mode = 2; } ///
初始化 DictionaryEx 设定getValue委托,key不存在时执行委托,并加入集合 /// ///
获取值的委托 ///
比较键时要使用对象 ///
集合是否限制外部修改 public DictionaryEx(Converter
getValue, IEqualityComparer
comparer, bool isReadOnly) { Assertor.AreNull(getValue, "getValue"); _items = new Dictionary
(comparer); _getValue = getValue; IsReadOnly = isReadOnly; _mode = 2; } ///
初始化 DictionaryEx 设定getValue委托,key不存在时执行委托,并加入集合 /// ///
获取值的委托 ///
内部字典 public DictionaryEx(Converter
getValue, IDictionary
dictionary) { Assertor.AreNull(getValue, "getValue"); Assertor.AreNull(dictionary, "dictionary"); _items = dictionary; _getValue = getValue; IsReadOnly = true; _mode = 2; } ///
初始化 DictionaryEx 设定getValue委托,key不存在时执行委托,并加入集合 /// ///
获取值的委托 ///
内部字典 ///
是否只读 public DictionaryEx(Converter
getValue, IDictionary
dictionary, bool isReadOnly) { _items = dictionary; _getValue = getValue; IsReadOnly = isReadOnly; _mode = 2; } ///
初始化 DictionaryEx 只读集合 /// ///
内部字典 public DictionaryEx(IDictionary
dictionary) { Assertor.AreNull(dictionary, "dictionary"); IsReadOnly = true; _items = dictionary; _mode = 0; } #endregion private TValue ReturnValue(TKey key) { switch (_mode) { case 1: return _defaultValue; case 2: var value = _getValue(key); lock (this) { _items[key] = value; } return value; default: throw new KeyNotFoundException(); } } public void Add(TKey key, TValue value) { this[key] = value; } public bool ContainsKey(TKey key) { return _items.ContainsKey(key); } public ICollection
Keys { get { return _items.Keys; } } public bool Remove(TKey key) { Assertor.AreTrue(IsReadOnly, "集合为只读"); return _items.Remove(key); } public bool TryGetValue(TKey key, out TValue value) { return TryGetValue(key, out value); } public ICollection
Values { get { return _items.Values; } } public TValue this[TKey key] { get { TValue value; if (_items.TryGetValue(key, out value)) { return value; } return ReturnValue(key); } set { Assertor.AreTrue(IsReadOnly, "集合为只读"); _items[key] = value; } } public void Add(KeyValuePair
item) { this[item.Key] = item.Value; } public void Clear() { Assertor.AreTrue(IsReadOnly, "集合为只读"); _items.Clear(); } public bool Contains(KeyValuePair
item) { return _items.Contains(item); } public void CopyTo(KeyValuePair
[] array, int arrayIndex) { ((IDictionary
)_items).CopyTo(array, arrayIndex); } public int Count { get { return _items.Count; } } public bool IsReadOnly { get; private set; } public bool Remove(KeyValuePair
item) { return Remove(item.Key); } public IEnumerator
> GetEnumerator() { return _items.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return _items.GetEnumerator(); } }
View Code

调用

static void Main(string[] args)        {            //key不存在返回默认值 ,key不区分大小写 (构造函数重载可以设定内部集合,是否只读)            var dict = new DictionaryEx
("default", StringComparer.OrdinalIgnoreCase); dict.Add("AAA", "aaa"); Console.WriteLine(dict["aAa"]); //aaa Console.WriteLine(dict["Bbb"]); //default //key不存在,执行委托,返回value,并加入集合 , 集合本身为只读 (构造函数重载可以设定内部集合,key比较方式) dict = new DictionaryEx
(key => "[" + key + "]", true); Console.WriteLine(dict["Bbb"]); //[Bbb] try { dict["Bbb"] = "newvalue"; //throw new NotSupportedException("集合为只读"); } catch (Exception) { } //创建只读键值对集合 var innerDict = new Dictionary
(); dict = new DictionaryEx
(innerDict); innerDict.Add("aaa", "aaa"); Console.WriteLine(dict["aaa"]); try { dict["Bbb"] = "newvalue"; //throw new NotSupportedException("集合为只读"); } catch (Exception) { } try { Console.WriteLine(dict["bbb"]); //throw new KeyNotFoundException(); } catch (Exception) { } }

Code CSDN

转载地址:http://uuxgx.baihongyu.com/

你可能感兴趣的文章
linux中单独编译网卡驱动
查看>>
结合zabbix监控系统io相关性能服务
查看>>
cve-2014-8517 FTP漏洞详解
查看>>
apache优化的“增加连接数”没学明白
查看>>
nmap 简单使用
查看>>
TortoiseGit日常使用指南
查看>>
ubuntu安装mongodb
查看>>
MVC4 EF linq从客户端中检测到有潜在的危险的Request.Path值
查看>>
MariaDB数据库备份与恢复
查看>>
Struts2中checkboxlist标签——应用、实现换行
查看>>
我的友情链接
查看>>
MySQL字符类型存放不同字符所占字节问题确认
查看>>
力战SDRAM(三)
查看>>
Lync 2010 学习(十三),手机登陆过程
查看>>
Datatables的自定义columns渲染与事件注册冲突解决
查看>>
异步获取EJB 服务实例
查看>>
######建立两台主机之间的ssh信任通道
查看>>
windows server迁移工具
查看>>
PHP连接mysql时mysql_connect()函数不可用,报500错误
查看>>
php console
查看>>