Example of a C # collection

Source: Internet
Author: User
Tags foreach
Using System;
Using System.Collections.Generic;
Using System.Text; Namespace ConsoleApplication1 {class Program {static void Main (string[] args) {//C
            Reate 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 a 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 is another name for the indexer and so I/can omit its name when accessing elements.
            Console.WriteLine ("For key = \ rtf\", value = {0}. ", openwith[" RTF "]);
            The indexer can is used to change the value associated//with a key.
            Openwith["rich-text"] = "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 a exception if the requested key is//isn't 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 is not to//is in the dictionary, TryGetValue can be a mor
            e 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 your 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}, V Alue = {1} ", kvp. Key, kvp.
            Value);
            }//To get the values alone and use the Values property. Dictionary<string, String>
            ValueCollection valuecoll = openwith.values; The elements of the valuecollection are strongly typed//with the type this is specified for dictionary V
            Alues.
            Console.WriteLine ();
            foreach (string s in Valuecoll) {Console.WriteLine ("Value = {0}", s);
            //To get the keys alone and use the keys. Dictionary<string, String>
            KeyCollection keycoll = Openwith.keys; The elements of the keycollection are strongly typed//with the type is 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 (); }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.