知识点总结: c#,使用自定义类型来作为Dictionary的Key
来源:互联网 发布:javascript高级教程 编辑:程序博客网 时间:2024/05/29 19:34
首先来看一个变量的定义:
/// <summary> /// key依次是StationId,channelId,deviceId,paraType,dataId,dataTypeId,logicalDeviceIndex,paraHandle,sourceId,value是paraName。读取PC_TB_35_07表后,再找到相应的SourceId后,保存到这里.保存的是定时记录 /// </summary> private Dictionary <uint, Dictionary <uint, Dictionary<uint, Dictionary<int, Dictionary<int, Dictionary<int, Dictionary<int, Dictionary<int, Dictionary<int, string>>>>> >>>> m_PC_TB_35_07DatalogDic = new Dictionary <uint, Dictionary <uint, Dictionary<uint, Dictionary<int, Dictionary<int, Dictionary<int, Dictionary<int, Dictionary<int, Dictionary<int, string>>>>> >>>>();
看不懂吧?看起来很困难吧。其实就是因为key有多个字段,但是写代码的人并没有想到可以使用使用自定义类型来作为key,所以就使用Dictionary套Dictionary的方式,这种方式会导致用的时候也很痛苦,例如,要一层一层的判断,这样就导致了每次使用,要做n次字典的查找,我数了一下,HasDatalog的一次调用,会导致17次的字典查找,实际上只需要一两次就够了。
public bool HasDatalog(uint stationId, uint channelId, uint deviceId) { if (!GetDatalogForChannel(stationId, channelId)) return false; if (!m_PC_TB_35_07DatalogDic.ContainsKey(stationId)) return false; if (!m_PC_TB_35_07DatalogDic[stationId].ContainsKey(channelId)) return false; if (!m_PC_TB_35_07DatalogDic[stationId][channelId].ContainsKey(deviceId)) return false; if (!m_PC_TB_35_07DatalogDic[stationId][channelId][deviceId].ContainsKey(1)) return false; if (m_PC_TB_35_07DatalogDic[stationId][channelId][deviceId][1].Count == 0) return false; return true; }
所以考虑使用自定义结构来作为Dictionary的Key,示例代码如下,其实主要就是要让自定义类型实现IEqualityComparer接口,而这里面最关键的就是要实现GetHashCode,好在这个结构的字段不多,所以就参考一下《C#本质论(第三版)》p267的实现,大致的代码如下。
using System.Collections.Generic;namespace TestDictionary{ struct DatalogValue { public int paraHandle; public int paraName; } struct DatalogKey : IEqualityComparer<DatalogKey> { public DatalogKey(uint dataId, uint dataTypeId, uint logicalDeviceIndex) { this.dataId = dataId; this.dataTypeId = dataTypeId; this.logicalDeviceIndex = logicalDeviceIndex; } public uint dataId; public uint dataTypeId; public uint logicalDeviceIndex; public bool Equals(DatalogKey x, DatalogKey y) { return x.dataId == y.dataId && x.dataTypeId == y.dataTypeId && x.logicalDeviceIndex == y.logicalDeviceIndex; } public int GetHashCode(DatalogKey obj) { int hashCode = dataId.GetHashCode(); if (hashCode != dataTypeId.GetHashCode()) hashCode ^= dataTypeId.GetHashCode(); if (hashCode != logicalDeviceIndex.GetHashCode()) hashCode ^= dataTypeId.GetHashCode(); return hashCode; } }}
测试:
using System;using System.Collections.Generic;using System.Windows.Forms;namespace TestDictionary{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Dictionary<DatalogKey, int> testDataLog = new Dictionary<DatalogKey, int>(); testDataLog.Add(new DatalogKey(1, 1, 1), 1); testDataLog.Add(new DatalogKey(1, 2, 1), 2); testDataLog.Add(new DatalogKey(1, 3, 1), 3); testDataLog.Add(new DatalogKey(2, 1, 1), 4); testDataLog.Add(new DatalogKey(2, 2, 1), 4); MessageBox.Show(testDataLog[new DatalogKey(2, 1, 1)].ToString()); if(!testDataLog.ContainsKey(new DatalogKey(2,1,2))) MessageBox.Show("no"); } }}
0 0
- 知识点总结: c#,使用自定义类型来作为Dictionary的Key
- Dictionary使用自定义类型为KEY
- 使用用户自定义类型作为std::map的key
- .Net中自定义类作为Dictionary的key详解
- Java用自定义的类型作为HashMap的key
- 【术】c#字典Dictionary自定义类作为key键
- 【C++】自定义结构体作为map的key
- hash_map自定义key类型的使用
- Struct vs Class 作为HashTable或者Dictionary的Key
- 使用Dictionary作为数据库应用的返回。
- C#:Dictionary<key,key>的Clear到底有多多块
- Dictionary字典类型的使用
- 自定义类型作为HashMap或HashTable的key需要注意哪些问题
- Java 自定义类作为HashMap的key
- 使用用户自定义类型作为std::map的…
- 使用std::set作为用户自定义类型的…
- HashCode 和 Equals 的使用 - 使用自定义对象作为HashMap的Key例子
- Objective-C如何使自定义对象类作为NSDictionary的key
- 使用testFlight进行企业部署
- vs2003 常见问题及处理
- 【JavsScript高级程序设计】学习笔记[1]
- 写时拷贝
- 一致哈希算法Java实现
- 知识点总结: c#,使用自定义类型来作为Dictionary的Key
- 存储限定符volatile
- jsp 向 js 传送数据
- (思维导图)第四章:字符串和多维数组
- 端口转发工具使用
- java 工程获取路径 web根目录。 项目classpath根目录 各种实用方法
- Linux命令整理-grep
- TextFiled 不响应点击
- Nutch2.0 之 Apache Gora 介绍