使用结构struct作为Dictionary<TKey,TValue>的键

我们经常用简单数据类型,比如int作为泛型Dictionary<TKey,TValue>的key,但有时候我们希望自定义数据类型作为Dictionary<TKey,TValue>的key,如何做到?

如果我们想自定义一个struct类型作为key,就必须针对该struct定义一个实现IEqualityComparer<T>接口的比较类,实现该接口的2个方法:Equals()方法和GetHashCode()方法,前者用来比较两个key是否相等,后者用来获取key的哈希值。

模拟这样一个场景:当我们去商场购物,经常需要把随身物品存放到某个储物柜,然后拿着该储物柜的钥匙。把钥匙抽象成key,不过,稍后会定义成一个struct类型的key,把随身物品抽象成值,那么所有的储物柜就是一个Dictionary<TKey,TValue>键值对集合。

定义一个struct类型的key,并且针对该struct定义一个比较类。

public struct GoodsKey
    {
        private int _no;
        private int _size;

        public GoodsKey(int no, int size)
        {
            _no = no;
            _size = size;
        }

        public class EqualityComparer : IEqualityComparer<GoodsKey>
        {

            public bool Equals(GoodsKey x, GoodsKey y)
            {
                return x._no == y._no && x._size == y._size;
            }

            public int GetHashCode(GoodsKey obj)
            {
                return obj._no ^ obj._size;
            }
        }
    }

随身物品抽象成如下。

public class Goods
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

客户端。

class Program
    {
        static void Main(string[] args)
        {
            Dictionary<GoodsKey, Goods> list = new Dictionary<GoodsKey, Goods>(new GoodsKey.EqualityComparer());
            GoodsKey key1 =new GoodsKey(1, 100);
            list.Add(key1,new Goods(){Id = 1, Name = "手表"});
            if (list.ContainsKey(key1))
            {
                Console.WriteLine("此柜已经本占用~~");
            }
            else
            {
                Console.WriteLine("此柜目前是空的~~");
            }
            Console.ReadKey();
        }
    }

运行,输出:此柜已经本占用~~

以上,在实例化Dictionary<GoodsKey, Goods>的时候,需要在其构造函数指明实现IEqualityComparer<GoodsKey>的比较类EqualityComparer实例。

虽然,实现了struct类型作为Dictionary<TKey,TValue>的key。但这其中存在着一些可以避免的"装箱、拆箱",优化方案可参考Jeffrey Zhao的博文,在这里

使用结构struct作为Dictionary<TKey,TValue>的键

时间: 2024-08-03 22:53:01

使用结构struct作为Dictionary<TKey,TValue>的键的相关文章

.net源码分析 – Dictionary&lt;TKey, TValue&gt;

接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blob/master/src/System.Collections/src/System/Collections/Generic/Dictionary.cs 接口 Dictionary<TKey, TValue>和List<T>的接口形式差不多,不重复说了,可以参考List<T>

.NET中Dictionary&lt;TKey, TValue&gt;浅析

.NET中Dictionary<TKey, Tvalue>是非常常用的key-value的数据结构,也就是其实就是传说中的哈希表..NET中还有一个叫做Hashtable的类型,两个类型都是哈希表.这两个类型都可以实现键值对存储的功能,区别就是一个是泛型一个不是并且内部实现有一些不同.今天就研究一下.NET中的Dictionary<TKey, TValue>以及一些相关问题. guid:33b4b911-2068-4513-9d98-31b2dab4f70c 文中如有错误,望指出.

Dictionary&lt;TKey, TValue&gt; 类

C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及其相关联的键组成.通过key检索值的速度非常快,其时间复杂度为常数阶 O(1),因为 Dictionary<TKey, TValue> 类是以哈希表的方式实现的. 只要对象用作键在 Dictionary<TKey, TValue>,不得更改任何会影响其哈希值的方式.每个在 Dictio

关于C#中 List&lt;T&gt; 和 Dictionary&lt;TKey, TValue&gt;组织数据效率和查询数据效率的比较。

最近我接受到一个工作任务,大致是做个通用的数据接口,供业务人员调用,要求返回前台所有的字段中文说明和字段对应的单位说明. 于是乎,我就定义了一个结构体: public class FieldInfo { public string FieldCode = string.Empty;//数据库字段 public string FieldName = string.Empty;//字段对应的中文说明 public string FieldUnit = string.Empty;//字段对应的单位说明

c# 扩展方法奇思妙用基础篇五:Dictionary&lt;TKey, TValue&gt; 扩展

Dictionary<TKey, TValue>类是常用的一个基础类,但用起来有时确不是很方便.本文逐一讨论,并使用扩展方法解决. 向字典中添加键和值 添加键和值使用 Add 方法,但很多时候,我们是不敢轻易添加的,因为 Dictionary<TKey, TValue>不允许重复,尝试添加重复的键时 Add 方法引发 ArgumentException. 大多时候,我们都会写成以下的样子: var dict = new Dictionary<int, string>()

C#高级编程五十三天----字典Dictionary&lt;TKey,TValue&gt;

字典 关键字:Dicitionary 说明: 必须包含命名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由两个元组组成:键和值). 键必须是唯一的,而值不需要唯一的. 键和值都可以是任意类型(例如:string,int,自定义类型,等等) 通过一个键读取一个值的事件是接近O(1) 键值对之间的偏序可以不定义 使用案例: using System; using System.Collections.Generic; using Syst

自定义一个可以被序列化的泛型Dictionary&lt;TKey,TValue&gt;集合

Dictionary是一个键值类型的集合.它有点像数组,但Dictionary的键可以是任何类型,内部使用Hash Table存储键和值.本篇自定义一个类型安全的泛型Dictionary<TKey, TValue>,并且可以被序列化. 为了使自定义的泛型Dictionary<TKey, TValue>可以被序列化成xml,需要实现泛型IXmlSerializable接口. public class MySerializableDictionary<TKey, TValue&g

C#中数组、集合(ArrayList)、泛型集合List&lt;T&gt;、字典(dictionary&lt;TKey,TValue&gt;)全面对比

为什么把这4个东西放在一起来说,因为c#中的这4个对象都是用来存储数据的集合--. 首先咱们把这4个对象都声明并实例化一下: //数组 string[] m_Str = new string[5]; //集合 ArrayList m_AList = new ArrayList(); //泛型集合 List<int> m_List = new List<int>(); //字典 Dictionary<int, string> m_Dt = new Dictionary&l

C#编程(五十三)----------字典Dictionary&lt;TKey,TValue&gt;

字典 关键字:Dicitionary 说明: 必须包含命名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由两个元组组成:键和值). 键必须是唯一的,而值不需要唯一的. 键和值都可以是任意类型(例如:string,int,自定义类型,等等) 通过一个键读取一个值的事件是接近O(1) 键值对之间的偏序可以不定义 使用案例: using System; using System.Collections.Generic; using Syst