·天新网首页·加入收藏·设为首页·网站导航
数码笔记本手机摄像机相机MP3MP4GPS
硬件台式机网络服务器主板CPU硬盘显卡
办公投影打印传真
家电电视影院空调
游戏网游单机动漫
汽车新车购车试驾
下载驱动源码
学院开发设计
考试公务员高考考研
业界互联网通信探索
您现在的位置:天新网 > 软件开发 > .Net开发 > C#
C#集合示例
http://www.21tx.com 2008年10月24日 EasyData

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create a new dictionary of strings, with string keys.
      //
      Dictionary<string, string> openWith =
        new Dictionary<string, string>();
      // Add some elements to the dictionary. There are no
      // duplicate keys, but some of the values are duplicates.
      openWith.Add("txt", "notepad.exe");
      openWith.Add("bmp", "paint.exe");
      openWith.Add("dib", "paint.exe");
      openWith.Add("rtf", "wordpad.exe");
      // The Add method throws an exception if the new key is
      // already in the dictionary.
      try
      {
        openWith.Add("txt", "winword.exe");
      }
      catch (ArgumentException)
      {
        Console.WriteLine("An element with Key = "txt" already exists.");
      }
      // The Item property is another name for the indexer, so you
      // can omit its name when Accessing elements.
      Console.WriteLine("For key = "rtf", value = {0}.",
        openWith["rtf"]);
      // The indexer can be used to change the value associated
      // with a key.
      openWith["rtf"] = "winword.exe";
      Console.WriteLine("For key = "rtf", value = {0}.",
        openWith["rtf"]);
      // If a key does not exist, setting the indexer for that key
      // adds a new key/value pair.
      openWith["doc"] = "winword.exe";
      // The indexer throws an exception if the requested key is
      // not in the dictionary.
      try
      {
        Console.WriteLine("For key = "tif", value = {0}.",
          openWith["tif"]);
      }
      catch (KeyNotFoundException)
      {
        Console.WriteLine("Key = "tif" is not found.");
      }
      // When a program often has to try keys that turn out not to
      // be in the dictionary, TryGetValue can be a more efficient
      // way to retrieve values.
      string value = "";
      if (openWith.TryGetValue("tif", out value))
      {
        Console.WriteLine("For key = "tif", value = {0}.", value);
      }
      else
      {
        Console.WriteLine("Key = "tif" is not found.");
      }
      // ContainsKey can be used to test keys before inserting
      // them.
      if (!openWith.ContainsKey("ht"))
      {
        openWith.Add("ht", "hypertrm.exe");
        Console.WriteLine("Value added for key = "ht": {0}",
          openWith["ht"]);
      }
      // When you use foreach to enumerate dictionary elements,
      // the elements are retrieved as KeyValuePair objects.
      Console.WriteLine();
      foreach (KeyValuePair<string, string> kvp in openWith)
      {
        Console.WriteLine("Key = {0}, Value = {1}",
          kvp.Key, kvp.Value);
      }
      // To get the values alone, use the Values property.
      Dictionary<string, string>.ValueCollection valueColl =
        openWith.Values;
      // The elements of the ValueCollection are strongly typed
      // with the type that was specified for dictionary values.
      Console.WriteLine();
      foreach (string s in valueColl)
      {
        Console.WriteLine("Value = {0}", s);
      }
      // To get the keys alone, use the Keys property.
      Dictionary<string, string>.KeyCollection keyColl =
        openWith.Keys;
      // The elements of the KeyCollection are strongly typed
      // with the type that was specified for dictionary keys.
      Console.WriteLine();
      foreach (string s in keyColl)
      {
        Console.WriteLine("Key = {0}", s);
      }
      // Use the Remove method to remove a key/value pair.
      Console.WriteLine("nRemove("doc")");
      openWith.Remove("doc");
      if (!openWith.ContainsKey("doc"))
      {
        Console.WriteLine("Key "doc" is not found.");
      }
      Console.ReadKey();
    }
  }
}

上一篇: C#访问数据库类(1)
下一篇: C#访问数据库类(2)

关于我们 | 联系我们 | 加入我们 | 广告服务 | 投诉意见 | 网站导航
Copyright © 2000-2011 21tx.com, All Rights Reserved.
晨新科技 版权所有 Created by TXSite.net