C# Dictionary操作范例(入门新手参考)
本文导语: 朋友推荐的一段代码,不敢独享,分享出来,供学习c#的朋友参考。 代码如下: 代码示例: /// /// c# Dictionary操作范例 /// by http://www. class Dirctonary { public void DictionaryGet() { Dictionary productList = new System.Collections.Generic.Dictio...
朋友推荐的一段代码,不敢独享,分享出来,供学习c#的朋友参考。
代码如下:
///
/// c# Dictionary操作范例
/// by http://www.
class Dirctonary
{
public void DictionaryGet()
{
Dictionary productList = new System.Collections.Generic.Dictionary();
productList.Add(1, "ProductionOne");
productList.Add(2, "ProductionTwo");
foreach (KeyValuePair production in productList)
{
MessageBox.Show(string.Format("{0},{1}", production.Key, production.Value));
}
//MessageBox.Show(productList.Count.ToString());
//MessageBox.Show(productList[1].ToString());
Dictionary.KeyCollection keys = productList.Keys;
foreach (var item in keys)
{
MessageBox.Show(item.ToString());
}
Dictionary.ValueCollection collection = productList.Values;
foreach (var item in collection)
{
MessageBox.Show(string.Format("{0}", item));
}
//productList.Remove(1);
//productList.Clear();
MessageBox.Show("判断是否包含键值对中的键为”1“的值");
if (productList.ContainsKey(1))
{
MessageBox.Show(productList[1]);
}
MessageBox.Show("判断是否包含键值对中的值为”ProductionTwo“的值");
if (productList.ContainsValue("ProductionTwo"))
{
MessageBox.Show(string.Format("{0}", "this really exists"));
}
}