Collection provides a way to organize arbitrary objects in a structured manner, and we have long known the importance of collection in daily programming .. The. NET Class Library provides a wide range of collection data types, which can even be seen by many people. These collection objects have their own special occasions. In any case, more choices mean higher flexibility, but also higher complexity. Therefore, it is necessary to have a moderate understanding of the usage and conditions of each type of set. Let's take a. Net collection tour with me!
. Net collection class definition
From. from the net perspective, the so-called set can be defined as an object, which implements one or more systems. collections. icollection, system. collections. idictionary and system. collections. ilist interface. This definition divides the "built-in" set in the system. Collections namespace into three categories:
◆ Ordered set: only sets of icollection interfaces are implemented. Normally, the insertion sequence of data items controls the order in which objects are retrieved from the set. The system. Collections. Stack and system. Collections. Queue classes are typical examples of icollection.
◆ Index set: the set of ilist, whose content can be retrieved from the number starting from scratch, just like an array. The system. Collections. arraylist object is an example of an index set.
◆ Key-type set: a set that implements the idictionary interface, including items that can be retrieved by certain types of key values. The content of the idictionary set is usually stored in key-value mode and can be sorted and retrieved by enumeration. The system. Collections. hashtable class implements the idictionary interface.
As you can see, the functions of a given set are largely controlled by specific interfaces or their Implemented interfaces. If you are not familiar with object-oriented programming, you may be hard to understand the above. However, you should at least know that the function of constructing objects using interfaces not only creates an object family with a complete set of similar methods, but also allows these objects to be treated as similar objects if necessary, in OOP (Object-Oriented Programming) terms, this is a well-known polymorphism technology.
System. Collections Overview
The System. Collections namespace contains six built-in General Collections that can be used in your application. Other more Specialized Collections belong to System. Collections. Specialized. In some cases, you may find these Specialized Collections very useful. With some exception classes added, these specialized collections are functionally similar to the built-in collections. Now let's take a look at General collections and a small number of less professional collections.
Stack and queue
The System. Collections. Stack and System. Collections. Queue classes only implement the ICollection interface and save the System. Object Type projects in the order that the storage items are added to the set. Objects can only be retrieved from the collection in the order they are added: the stack is post-in-first-out, while the queue is first-in-first-out. Generally, you can consider using these collections in the following scenarios:
◆ The order of receiving and processing items in the set is important.
◆ You can discard the project after processing it.
◆ You do not need to access any project in the set.
Arraylist
The System. Collections. ArrayList class only implements Ilist. It is best to describe a hybrid type of a normal array and a set. ArrayList stores the project in the order that the project is added to the set. Each item is assigned an index identifier and can be retrieved in any order by the index numbers associated with them. When a new project is added to a set, the ArrayList is expanded to make it more flexible than a normal array. However, the ArrayList load is larger than the traditional array and does not implement strict typing. In this way, you can accept any Object converted to System. Object (in other words, do not reject anything ).
Sortedlist
System. Collections. SortedList, which implements the IDictionary and ICollection interfaces, is the most basic sortset combination, and is very similar to the Collection object in Vb6. SortedList stores objects and sorts them by their associated key values. They are also the only built-in. NET set that supports both index numbers and key object retrieval.
Hashtable
The powerful system. Collections. hashtable collection implements idictionary and icollection, which can be used to store various types of objects together with the associated unique string key values. Projects in the hashtable set are stored in the order determined by the hash code from their key values. The key value of each object in the set must be unique, and its hash code is not necessarily unique.
What is hash code?
In essence, the hash code is the result of eliminating all redundant parts from the fast data. It mainly plays a role in secondary data classification or sorting.
When a project is added to a set, hashtable calls the gethashcode method of the key value, because all classes are from the system. objec inherits, so you can call this method to determine the hash code of the class and store it in a sort order. You can force the use of custom hash functions. There are two methods: one is the gethashcode method of the overload class, and the other is to pass the system to the hashtable constructor. collections. ihashcodeprovider interface. In this case, this object is used to generate a hash code for all key values added to the set.
In terms of performance, hashtable can quickly retrieve any element from the set because key value search is limited to key values with the same hash code, this reduces the number of key values that must be checked to find a match. However, because each object-key-Value Pair inserted into the set must generate corresponding hash code, the cost of project insertion is a bit high. Therefore, hashtable is mainly used to repeatedly retrieve a large amount of relatively static data based on any key value.
Listdictionary and hybriddictionary
The listdictionary and hybriddictionary classes belong to system. Collections. Specialized. They all organize projects according to the unique key value principle, and both implement idictionary and icollection. Listdictionary stores items in the form of a linked list internally. It is recommended to use it in a collection that will not grow more than 10 items. Hybriddictionary uses an internal chain table (actually a listdictionary) as a small set. When the set becomes large enough (more than 10 items), the linked list becomes less efficient and converts it to hashtable.
StringCollection and StringDictionary
System. Collections. Specialized. stringcollection and system. Collections. Specialized. stringdictionary both optimize the collection of stored strings. Stringcollection implements ilist and icollection and is essentially an arraylist. It only supports strings for strong typing. Stringcollection is ideal for applications where a small amount of data is frequently updated or added, while stringdictionary is best suited for a large amount of data that is not frequently added to a collection such as hashtable.
NameValueCollection
The most interesting thing about system. Collections. Specialized. namevaluecollection is that it can contain multiple projects associated with the same key value, which is the difference between it and other built-in collections. In addition, it is similar to hashtable in function. It sorts projects by the hash code derived from the key value of each project and has the advantages and disadvantages of similar.
. NET collection class problems
If the built-in set provided by the. NET Class Library also has a problem, most of them store the project internally as the system. Object. type. From the perspective of maximum flexibility, this is a good idea, but it also raises some questions for programmers who use these universal sets. First, as long as you add a new project to the collection, you must implement the type conversion operation during the runtime (create a value-type index so that it can be referenced as an object ). This is an inefficient operation and produces considerable performance problems when dealing with large sets. Second, as long as you access a project in a universal set, this project will be used as a system. the object type is returned, which means you have to convert it to a real type to perform meaningful operations on it.
The above is an introduction to the definition of. Net collection classes.