Esempio n. 1
0
        static void Main()
        {
            #region Ctor0
            // Instantiate with four ISO 3166-1 country codes:
            var cc = new RankedDictionary <string, string>
            {
                { "TO", "Tonga" },
                { "DD", "German Democratic Republic" },
                { "CH", "Switzerland" },
                { "RU", "Burundi" }
            };
            #endregion

            #region Add
            // The Add method throws an exception if the new key is
            // already in the dictionary.
            try
            {
                cc.Add("DD", "East Germany");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("An element with Key = 'DD' already exists.");
            }
            #endregion

            // The Item property is another name for the indexer,
            // so you can omit its name when accessing elements.
            Console.WriteLine($"For key = 'CH', value = {cc["CH"]}.");

            #region Indexer
            // The indexer can be used to change the value associated with a key.
            cc["RU"] = "Russian Federation";

            // The indexer can be used to get a value for a key.
            Console.WriteLine($"For key = 'RU', value = {cc["RU"]}.");

            // If a key does not exist, setting the indexer for that key
            // adds a new key/value pair.
            cc["SS"] = "South Sudan";
            #endregion

            // The indexer throws an exception if the supplied key is
            // not in the dictionary.
            try
            {
                Console.WriteLine($"For key = 'ZZ', value = {cc["ZZ"]}.");
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("Key = 'ZZ' is not found.");
            }

            #region TryGetValue
            // When a program often has to try keys that are usually not in the
            // dictionary, TryGetValue can be a more efficient way to get values.
            if (cc.TryGetValue("ZZ", out string value))
            {
                Console.WriteLine($"For key = 'ZZ', value = {value}.");
            }
            else
            {
                Console.WriteLine("Key = 'ZZ' is not found.");
            }
            #endregion

            #region ContainsKey
            // ContainsKey can be used to test keys before inserting them.
            if (!cc.ContainsKey("GG"))
            {
                cc.Add("GG", "Guernsey");
                Console.WriteLine($"Value added for key = 'GG': {cc["GG"]}");
            }
            #endregion

            // When you use foreach to enumerate dictionary elements,
            // the elements are retrieved as KeyValuePair instances.
            Console.WriteLine();
            foreach (KeyValuePair <string, string> pair in cc)
            {
                Console.WriteLine($"Key = {pair.Key}, Value = {pair.Value}");
            }

            #region Values
            // To get the values alone, use the Values property.
            RankedDictionary <string, string> .ValueCollection vals = cc.Values;

            // The elements of the ValueCollection are strongly typed
            // with the type that was specified for dictionary values.
            Console.WriteLine();
            foreach (string val in vals)
            {
                Console.WriteLine($"Value = {val}");
            }
            #endregion

            #region Keys
            // To get the keys alone, use the Keys property.
            RankedDictionary <string, string> .KeyCollection keys = cc.Keys;

            // The elements of the KeyCollection are strongly typed
            // with the type that was specified for dictionary keys.
            Console.WriteLine();
            foreach (string key in keys)
            {
                Console.WriteLine($"Key = {key}");
            }
            #endregion

            // Use the Remove method to remove a key/value pair.
            Console.WriteLine("\nRemoving 'DD'.");
            cc.Remove("DD");

            Console.WriteLine("\nChecking if 'DD' exists:");
            if (!cc.ContainsKey("DD"))
            {
                Console.WriteLine("  Key 'DD' not found.");
            }
        }
Esempio n. 2
0
        static void Main()
        {
            var dary = new RankedDictionary <int, int>()
            {
                Capacity = 4
            };

            for (int w = 1; w < 21; ++w)
            {
                foreach (Permutation permAdd in new Permutation(w).GetRows())
                {
                    foreach (Permutation permDel in permAdd.GetRows())
                    {
                        for (int m = 0; m < permAdd.Choices; ++m)
                        {
                            dary.Add(permAdd[m], permAdd[m] + 100);
#if DEBUG
                            if (permDel.Rank == 0)
                            {
                                try
                                {
                                    dary.SanityCheck();
                                }
                                catch (DataMisalignedException ex)
                                {
                                    Console.WriteLine("Insanity found: {0}", ex.Message);
                                    Console.WriteLine("Width={0} add.Rank={1} m={2}", w, permAdd.Rank, m);
                                    throw;
                                }
                            }
#endif
                        }

                        for (int m = 0; m < permDel.Choices; ++m)
                        {
                            dary.Remove(permDel[m]);
#if DEBUG
                            try
                            {
                                dary.SanityCheck();
                            }
                            catch (DataMisalignedException ex)
                            {
                                Console.WriteLine("Insanity found: {0}", ex.Message);
                                Console.WriteLine("Width={0} add.Rank={1} del.Rank={2} m={3}",
                                                  w, permAdd.Rank, permDel.Rank, m);
                                throw;
                            }
#endif
                        }

                        if (dary.Count != 0)
                        {
                            throw new DataMisalignedException("Count should be zero");
                        }
                        dary.Clear();
                    }
                }

                Console.WriteLine("{2} - Completed Width {0} = {1}", w, new Permutation(w), DateTime.Now);
                Thread.Sleep(250);
            }
        }