c# – ListDictionary类有一个通用的替代方法吗?

栏目: ASP.NET · 发布时间: 7年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/1234831/is-there-a-generic-alternative-to-the-listdictionary-class
我正在查看一些示例代码,并在其中使用一个ListDictionary对象来存储少量数据(约5-10个对象,但这个数字可能会随着时间而改变).使用这个类的唯一问题是,与我一直在做的其他事情不同,它不是通用的.这意味着,如果我在这里错了,就纠正我,每次我从这里得到一个对象,或者列举出一个对象,那就是投射.在较大的Dictionary<

T>中有足够的开销对象来证明非通用ListDictionary的开销?

将使用此对象的代码将枚举在每个页面加载上,我猜想是为什么ListDictionary类被用于其他替代方法之一.这也是为什么我希望在这个数据列表中表现最好的.

不幸的是没有类似于ListDictionary的泛型.

但是,实施这一点并不是非常困难的. ListDictionary基本上通过保留Key / Value对的链表,并对它们进行查找操作.您可以创建一个ListDictionary&TK; TKalue>通过包装LinkedList<T>有一些非常简单的LINQ表达式.

例如

public class LinkedDictionary<TKey,TValue> {
  private LinkedList<KeyValuePair<TKey,TValue>> _list = new LinkedList<KeyValuePair<TKey,TValue>>();
  private IEqualityComparer<TKey> _comp = EqualityComparer<TKey>.Default;

  public void Add(TKey key, TValue value) { 
    _list.Add(new KeyValuePair<TKey,TValue>(key,value)); 
  }
  public TValue Get(TKey key) {  
    return _list.Where(x => _comp.Equals(x.Key,key)).First().Value;
  }
  ...
}

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/1234831/is-there-a-generic-alternative-to-the-listdictionary-class


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Domain-Driven Design

Domain-Driven Design

Eric Evans / Addison-Wesley Professional / 2003-8-30 / USD 74.99

"Eric Evans has written a fantastic book on how you can make the design of your software match your mental model of the problem domain you are addressing. "His book is very compatible with XP. It is n......一起来看看 《Domain-Driven Design》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具