Understanding and using Hashtable containers in C #

Source: Internet
Author: User

Send a message about Hashtable

Recently, the training content involves Hashtable knowledge. As training only plays a guiding role and is rarely used in the past, it is quite unfamiliar with Hashtable, let's take a look at things tonight. You are more welcome to criticize and correct me.

What is Hashtable?

From the literal meaning, we can guess that Hashtable is a hash table, which implements a hash table through classes with key-value pairs stored in it. The hash table can map keys to corresponding values. Hashtable is a container. We can use the key to quickly find the corresponding value. In Hashtable, the key and value are of the object type. That is to say, Hashtable can support any type.

Simple operations on the Hashtable Method

Perform some simple operations on Hashtable and List objects. Note that Hashtable must be referenced when used.

           Main(              Hashtable ht =              ht.Add(,             ht.Add(,              ht.Add(,               j=ht[             ht.Remove(              j = ht[].ToString();                }

In Hashtable, we can add a key-value pair, remove a key-value pair, remove all key-value pairs, and determine whether a hash table contains a key-value pair. This sentence is not correct. Thank you for reminding us that if the key value is a numerical value, you can use Hashtable [0] to obtain its value. However, it is not recommended that you write this statement, so it is easy to confuse many indexes.

Simple operations on the Hashtable attribute

There are not many Hashtable attributes. For details, refer:

Experiment in person to deepen your impression:

           Main(              Hashtable ht =              ht.Add(,              ht.Add(,              ht.Add(,              ht.Add(,              ht.Add(,                count =             Console.WriteLine( + count +               isFixedSize =             Console.WriteLine( +              isReadOnly =             Console.WriteLine( +             ICollection keys =             ICollection values =              ( item                  ( item              }

The result is as follows:

Traversal of Hashtable <very practical>

As we mentioned earlier, Hashtable is a container, we should be able to traverse it and get all its keys and values, so I took it for granted that I wrote the following code before reading relevant knowledge:

              ( myitem                   Console.WriteLine(myitem.Key +  +             }

Obviously, after the test, the error is obvious, and the compilation fails at all. So how can we traverse the key-value pairs in Hashtable? I got some information through the Internet.

              (DictionaryEntry myitem                   Console.WriteLine(myitem.Key +  +             }

We can use the DictionaryEntry structure to set or retrieve dictionary key/value pairs. However, you need to know how to use foreach to read the key values in the hash table,ForeachA statement is a packaging of enumerative numbers. It can only be read from the set and cannot be written to the set. So when we set the value of the key-value pair again, an error will be reported during compilation.

Conclusion

The double break has passed, and the new business day has come again. There are more challenges waiting for me to continue.

------If you think this article is helpful to you, don't forget to click the recommendation in the lower right corner. Thank you!------

Related Article

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.