C# 键值对类相关 C#键值对容器的介绍
C# 键值对类相关
C# 键值对类相关
一 C# 键值对类有以下类:
① IDictionary<string, Object> idc = new Dictionary<string, object>();
② KeyValuePair<string, object> par = (KeyValuePair<string, object>)shoplistcomboBox.SelectedItem;
③ Hashtable ht=new Hashtable(); file创建一个Hashtable实例
ht.Add(E,e);添加keyvalue键值对
Hashtable 内的每一组对象就是一个DictionaryEntry
例如我们要循环hashtable
foreach (DictionaryEntry de in myHashtable) {...}
Hashtable就是一个DictionaryEntry的集合
二 KeyValuePair和Hashtable区别
KeyValuePair是单个的键值对对象,而Hashtable是一个集合。
KeyValuePair用于接收combox选定的值。
例如:KeyValuePair<string, object> par = (KeyValuePair<string, object>)shoplistcomboBox.SelectedItem;
三 hashtable 与 dictionary的区别
1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.
2:多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.
3:Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.
StringDictionary:默认key不区分大小写
NameValueCollection:默认key区分大小写
KeyedCollection:不是键值对容器,但是比键值对容器更好用,强烈推荐
命名空间using System.Collections.Specialized
System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。
System.Collections.Generic 命名空间包含定义泛型集合的接口和类,泛型集合允许用户创建强类型集合,它能提供比非泛型强类型集合更好的类型安全性和性能。
System.Collections.Specialized 命名空间包含专用的和强类型的集合,例如,链接的列表词典、位向量以及只包含字符串的集合。
Hashtable、SortedList
SortedList为可排序的字典,当添加元素时,元素按照正确的排序顺序插入SortedList,同时索引自动进行相应的调整,移除元素亦然。
Hashtable、SortedList的键和值均为object类型,因此使用的时候,转化比较频繁
dictionary
范型Dictionary,可以随便制定key,value的类型
Dictionary <String, String> dic = new Dictionary <string, string> ();
dic.Add( "1 ", "Jerry ");
dic.Add( "2 ", "Kimmy ");
dic.Add( "3 ", "Tommy ");
也可以自己定义类来使用
public class KeyValueItem
{
private int _Value;
public int Value
{
get
{
return _Value;
}
}
private string _Name;
public string Name
{
get
{
return _Name;
}
}
//
public KeyValueItem(string name, int value)
{
_Name = name;
_Value = //www.jb51.net/dgjack/archive/2012/03/03/value;
}
public override string ToString()
{
return _Name;
}
}
插入值的时候:
KeyValueItem it = new KeyValueItem("客户1", 1);
comboBox1.Items.Add(it);
it = new KeyValueItem("客户2", 2);
comboBox1.Items.Add(it);
it = new KeyValueItem("客户3", 3);
comboBox1.Items.Add(it);
取值的时候就用 :
int relationtype = ((KeyValueItem)comboBox1.SelectedItem).Value;
C#键值对容器
StringDictionary:默认key不区分大小写
NameValueCollection:默认key区分大小写
KeyedCollection:不是键值对容器,但是比键值对容器更好用,强烈推荐
命名空间using System.Collections.Specialized
System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。
System.Collections.Generic 命名空间包含定义泛型集合的接口和类,泛型集合允许用户创建强类型集合,它能提供比非泛型强类型集合更好的类型安全性和性能。
System.Collections.Specialized 命名空间包含专用的和强类型的集合,例如,链接的列表词典、位向量以及只包含字符串的集合。
Hashtable、SortedList
SortedList为可排序的字典,当添加元素时,元素按照正确的排序顺序插入SortedList,同时索引自动进行相应的调整,移除元素亦然。
Hashtable、SortedList的键和值均为object类型,因此使用的时候,转化比较频繁
dictionary
范型Dictionary,可以随便制定key,value的类型
Dictionary <String, String> dic = new Dictionary <string, string> ();
dic.Add( "1 ", "Jerry ");
dic.Add( "2 ", "Kimmy ");
dic.Add( "3 ", "Tommy ");
也可以自己定义类来使用
public class KeyValueItem
{
private int _Value;
public int Value
{
get
{
return _Value;
}
}
private string _Name;
public string Name
{
get
{
return _Name;
}
}
//
public KeyValueItem(string name, int value)
{
_Name = name;
_Value = http://www.cnblogs.com/dgjack/archive/2012/03/03/value;
}
public override string ToString()
{
return _Name;
}
}
插入值的时候:
KeyValueItem it = new KeyValueItem("客户1", 1);
comboBox1.Items.Add(it);
it = new KeyValueItem("客户2", 2);
comboBox1.Items.Add(it);
it = new KeyValueItem("客户3", 3);
comboBox1.Items.Add(it);
取值的时候就用 :
int relationtype = ((KeyValueItem)comboBox1.SelectedItem).Value;




浙公网安备 33010602011771号