示例#1
0
        public void UnitRdx_RemoveAt()
        {
            var rd = new RankedDictionary <int, int>();

            for (int ii = 0; ii < 5000; ++ii)
            {
                rd.Add(ii, -ii);
            }

            for (int i2 = 4900; i2 >= 0; i2 -= 100)
            {
                rd.RemoveAt(i2);
            }

            for (int i2 = 0; i2 < 5000; ++i2)
            {
                if (i2 % 100 == 0)
                {
                    Assert.IsFalse(rd.ContainsKey(i2));
                }
                else
                {
                    Assert.IsTrue(rd.ContainsKey(i2));
                }
            }
        }
示例#2
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.");
            }
        }
示例#3
0
 /// <summary>Determines whether the dictionary contains the supplied key.</summary>
 /// <param name="key">The key to locate.</param>
 /// <returns><b>true</b> if <em>key</em> is contained in the dictionary; otherwise <b>false</b>.</returns>
 /// <exception cref="ArgumentNullException">When <em>key</em> is <b>null</b>.</exception>
 public bool Contains(TKey key)
 => tree.ContainsKey(key);