System.Collections.Tests.Helpers.PerformActionOnAllHashtableWrappers C# (CSharp) Method

PerformActionOnAllHashtableWrappers() public static method

public static PerformActionOnAllHashtableWrappers ( Hashtable hashtable, Action action ) : void
hashtable Hashtable
action Action
return void
        public static void PerformActionOnAllHashtableWrappers(Hashtable hashtable, Action<Hashtable> action)
        {
            // Synchronized returns a slightly different version of Hashtable
            Hashtable[] hashtableTypes =
            {
                (Hashtable)hashtable.Clone(),
                Hashtable.Synchronized(hashtable)
            };

            foreach (Hashtable hashtableType in hashtableTypes)
            {
                action(hashtableType);
            }
        }

Usage Example

        public void ContainsValue()
        {
            Hashtable hash1 = Helpers.CreateStringHashtable(100);

            Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 =>
            {
                for (int i = 0; i < hash2.Count; i++)
                {
                    string value = "Value_" + i;
                    Assert.True(hash2.ContainsValue(value));
                }

                Assert.False(hash2.ContainsValue("Non Existent Value"));
                Assert.False(hash2.ContainsValue(101));
                Assert.False(hash2.ContainsValue(null));

                hash2.Add("Key_101", null);
                Assert.True(hash2.ContainsValue(null));

                string removedKey   = "Key_1";
                string removedValue = "Value_1";
                hash2.Remove(removedKey);
                Assert.False(hash2.ContainsValue(removedValue));
            });
        }
All Usage Examples Of System.Collections.Tests.Helpers::PerformActionOnAllHashtableWrappers