Dictionary

来源:互联网 发布:nginx php error log 编辑:程序博客网 时间:2024/04/28 11:44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

    class Program
    {
        static void Main(string[] args)
        {
            //文字列キーで文字列の新しい辞書を作成する
            Dictionary<string, string> openWith = new Dictionary<string, string>();
           
            //いくつかの要素を辞書に追加する。重複するキーはありませんが、値の一部は重複しています。
            openWith.Add("txt", "Notepad.exe");
            openWith.Add("bmp", "paint.exe");
            openWith.Add("dib", "paint.exe");
            openWith.Add("rtf", "wordpad.exe");
            //新しいキーが既に辞書にある場合は、Addメソッドが例外をスローする。
            try
            {
                openWith.Add("txt", "Winword.exe");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("An element with Key = \"txt\" already exists.");
            }




            Console.WriteLine("For Key =\"rtf\" , value={0}", openWith["rtf"]);
            Console.ReadKey();


            //インデクサを使用して、キーに関連付けられた値を変更することができる
            openWith["rtf"] = "winword.exe";


            Console.WriteLine("For Key=\"rtf\",value={0}", openWith["rtf"]);
            Console.ReadKey();


            //新しいKeyとして追加する、valueはwinword.exe
            openWith["doc"] = "winword.exe";


            try
            {
                Console.WriteLine("For Key=\"tif\",value={0}", openWith["tif"]);
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("key=\"tif\" is not found");
            }


            try
            {
                Console.WriteLine("For Key=\"rtf\",value={0}", openWith["rtf"]);
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("key=\"rtf\" is  override");
            }


            Console.ReadKey();
            try
            {
                Console.WriteLine("For Key=\"doc\",value={0}", openWith["doc"]);
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("key=\"doc\" is not add");
            }




            Console.ReadKey();


            string value = "";
            if (openWith.TryGetValue("tif", out value))
            {
                Console.WriteLine("For Key=\"tif\",value={0}", value);
            }
            else
            {
                Console.WriteLine("Key =\"tif\" is not found");
            }
            Console.ReadKey();


            //ContainsKeyはキーを挿入する前に、キーをテストするために使用できる
            if (!openWith.ContainsKey("ht"))
            {
                openWith.Add("ht", "hypertrm.exe");
                Console.WriteLine("value added for key=\"ht\":{0}", openWith["ht"]);
            }
            Console.ReadKey();


            //foreachを使用して、辞書要素を列挙する。要素はKeyValuePairオブジェクトとして取得される
            foreach (KeyValuePair<string, string> kvp in openWith)
            {
                Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
            }
            Console.ReadKey();


            // Valuesプロパティを使用、Valueだけを取得する
            Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
            foreach (string s in valueColl)
            {
                Console.WriteLine("Value{0}", s);
            }
            Console.ReadKey();


            //Keysプロパティを使用、キーだけを取得する
            Dictionary<string, string>.KeyCollection keyColl = openWith.Keys;
            foreach (string s in keyColl)
            {
                Console.WriteLine("key={0}", s);
            }
            Console.ReadKey();


            //Removeメソッドを使用、Key/Valueのペアを削除する
            Console.WriteLine("\n Remove(\"doc\")");
            openWith.Remove("doc");
            if (!openWith.ContainsKey("doc"))
            {
                Console.WriteLine("key \"doc\" is not found");
            }
            Console.ReadKey();
        }
    
}
原创粉丝点击