Learning the set of Key and Value hashtable

Keywords: C#

You can create a hashtable:

 

You can use foreach method to write out the key and value loops of hashtable:

 

Output on the console screen:

 

If you only need to output the key:

 

If you only want to cycle the value output:

 

Test output:

 

 

Add key and value to the hashtable set:

 

Add or remove:

 

Test Add and remove above:

 

The key value "A" has been removed.

 

Next, we will practice two methods to determine whether the key or avlue already has a set:

Complete exercise Code:

 class Av
    {
        public Hashtable HashtableEntity = new Hashtable()
        {
            { "A", "Leo" },
            { "B", "Insus.NET" },
            { "C", "Jack" }
        };


        public void Output()
        {
            foreach (DictionaryEntry de in HashtableEntity)
            {
                Console.WriteLine(string.Format("Key: {0}; Value: {1}", de.Key, de.Value));
            }
        }        

        public void ForeachKey()
        {
            foreach (string key in HashtableEntity.Keys)
            {
                Console.WriteLine(key);
            }
        }

        public void ForeachValue()
        {
            foreach (string value in HashtableEntity.Values)
            {
                Console.WriteLine(value);
            }
        }
        
        public void Add(string key, string value)
        {
            HashtableEntity.Add(key, value);
        }

        public void Remove(string key)
        {
            HashtableEntity.Remove(key);
        }

        public bool isExitByKey(string key)
        {
            return HashtableEntity.ContainsKey(key);
        }

        public bool isExistByValue(string value)
        {
            return HashtableEntity.ContainsValue(value);
        }

    }
Source Code

Posted by Lagreca on Sun, 31 May 2020 11:04:20 -0700