KeyValuePair VS DictionaryEntry

There are some difference between KeyValuePair which is the generic version and DictionaryEntry which is non generic version.

  1. KeyValuePair < T,T > is for iterating through Dictionary < T,T >. This is the .Net 2 way of doing things.
  2. DictionaryEntry is for iterating through HashTables. This is the .Net 1 way of doing things.
  3. KeyValuePair<TKey,TValue> is used in place of DictionaryEntry because it is generic.

The advantage of using a KeyValuePair<TKey,TValue> is that we can give the compiler more information about what is in our dictionary. Esp. the data type

Dictionary<string, int> dict = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> item in dict) {
  int i = item.Value;
}

Hashtable hashtable = new Hashtable();
foreach (DictionaryEntry item in hashtable) {
  // Cast required because compiler doesn‘t know it‘s a <string, int> pair.
  int i = (int) item.Value;
}
时间: 2024-12-29 11:38:59

KeyValuePair VS DictionaryEntry的相关文章

C# winform key value型数据如何绑定ComBox (hashtable,keyvaluepair,dictionary )

cbUserAgent是一个combox ArrayList list = new ArrayList(); Dictionary<string, string> useragents = new Dictionary<string, string>(); /// <summary> /// 初始化combox /// </summary> private void InitCbUserAgent() { cbUserAgent.Items.Clear();

C#基础第四天-作业答案-Hashtable-list&lt;KeyValuePair&gt;泛型实现名片

1.Hashtable 实现 Hashtable table = new Hashtable(); while (true) { Console.WriteLine("------------------1.增加--------------------"); Console.WriteLine("------------------2.查询--------------------"); Console.WriteLine("----------------

字典实体类:DictionaryEntry类

DictionaryEntry类是一个字典集合,主要包括的内容是键/值对.这样的组合方式能够方便地定位数据,当中的"键"具备唯一性,类似于数据库中的"id",一个id相应一天记录,而一个键仅仅相应一个值. 使用DictionaryEnry类能够方便地设置和检索数据.尽管被称为字典集合,但DictionaryEntry并不包括一组数据,而仅仅是一个"键/值"对,一般通过"IDictionaryEnumerator"."

Check if KeyValuePair exists with LINQ&#39;s FirstOrDefault

http://stackoverflow.com/questions/793897/check-if-keyvaluepair-exists-with-linqs-firstordefault 问题: I have a dictionary of type Dictionary<Guid,int> I want to return the first instance where a condition is met using var available = m_AvailableDict.

KeyValuePair C#

前几天自学了keyvaluepair,在网上找到一篇很好的Blog ,所以转载过来共享. 转载地址:http://www.cnblogs.com/ C# KeyValuePair KeyValuePair stores two values together. It is a single generic struct. The KeyValuePair type in System.Collections.Generic is simple and always available. It i

.Net数据结构:DictionaryBase DictionaryEntry

DictionaryBase 类是用来创建用户字典的抽象类. 而字典则是利用散列表 (或者有时为单独的链表)作为潜在的数据结构来把数据存储到键值对内的一种数据结构.键值对作 为DictionaryEntry 对 象 来 进 行 存 储 , 而 且 必 须 使 用 Key 方 法 和 Value 方 法 来 取 回DictionaryEntry 对象中的实际值. public class myDictionary : DictionaryBase { public myDictionary() {

KeyValuePair

KeyValuePair用法(转)(2012-06-25 10:47:35) 转载▼ 标签: keyvaluepair it   KeyValuePair C# KeyValuePair<TKey,TValue>的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和Name(string类型)这样的键/值对,那么可以这样使用. /// <summary>/// 设置键/值对/// </summary&

使用泛型 类型System.Collections.Generic.KeyValuePair需要2个类型参数

c#在准备使用System.Collections.Generic.KeyValuePair遍历的Hashtable时候,代码却提示有错误: 这其实是个非常简单的错误,Eclipse中通过工具弹出的提示解决方法,点下鼠标就能解决问题的,Visual Studio 2010却不行. 不过提示也说的比较清楚了,需要指定两个参数的类型. 根据你的Hashtable中保存的Key和Value的数据类型指定就可以了. 例如: KeyValuePair<string, int> KeyValuePair&

KeyValuePair用法(转)

转载自:http://blog.sina.com.cn/s/blog_9741eba801016w61.html C# KeyValuePair<TKey,TValue>的用法.结构体,定义可设置或检索的键/值对.也就是说我们可以通过 它记录一个键/值对这样的值.比如我们想定义一个ID(int类型)和Name(string类型)这样的键/值对,那么可以这 样使用. /// <summary>/// 设置键/值对/// </summary>/// <returns&