在C#中多键的字典(的另一种)? [英] Multi-key dictionaries (of another kind) in C#?

查看:793
本文介绍了在C#中多键的字典(的另一种)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题,有没有对具有多一个简单的解决方案关键dictionary,它的无论是单独键的可用于识别价值?

  MultikeyDictionary< TKey1,TKey2,TValue>富;
foo.Add(KEY1,KEY2,价值);
myvalue的= foo的[键1];
//值== myvalue的
foo.Remove(密钥);
myvalue的= foo的[键1]; //无效,异常或空返回


解决方案

这个博客帖子似乎对细节相当体面的实现。


  对于C#

多键泛型类Dictionary


  
  

MultiKeyDictionary是一个C#类
  它包装并扩展了通用
  所提供的Dictionary对象
  微软在.NET 2.0及以上。这个
  允许开发人员创建一个通用
  价值和参考的字典
  通过两个键而不是值列表
  只是由微软提供一
  实现通用的
  字典< ...>。你可以看到我
  文章$ C $的CProject(在这里),然而,
  这code更先进的数据和错误
  免费的。


Building on this question, is there a simple solution for having a multi-key dictionary where either key individually can be used to identify the value?

ie.

MultikeyDictionary<TKey1, TKey2, TValue> foo;
foo.Add(key1, key2, value);
myValue = foo[key1];
// value == myValue
foo.Remove(key2);
myValue = foo[key1]; // invalid, Exception or null returned

解决方案

This blog post seems to detail a rather decent implementation.

Multi-key generic dictionary class for C#

MultiKeyDictionary is a C# class that wraps and extends the Generic Dictionary object provided by Microsoft in .NET 2.0 and above. This allows a developer to create a generic dictionary of values and reference the value list through two keys instead of just the one provided by the Microsoft implementation of the Generic Dictionary<...>. You can see my article on CodeProject (here), however this code is more up-to-date and bug free.

这篇关于在C#中多键的字典(的另一种)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆