Hashtable, ArrayList, List, Dictionary Learning

Source: Internet
Author: User
Hashtable usage
In. in the. NET Framework, Hashtable is System. A container provided by the Collections namespace is used to process and present key-value pairs similar to key/value. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In Hashtable, key/value pairs are of the object type, so Hashtable can support any type of key/value pairs ..
Add a key/value pair in the hash table: HashtableObject. Add (key, value );
Remove a key/value pair in the hash table: HashtableObject. Remove (key );
Remove all elements from the hash table: HashtableObject. Clear ();
Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );

Hashtable ht = new Hashtable ();
Ht. Add ("a", 123 );
Ht. Add ("B", 456 );

// DictionaryEntry Object is required to traverse the hash table
Foreach (DictionaryEntry de in ht)
{
MessageBox. Show (de. Key. ToString () + "" + de. Value. ToString ());
}

// Sort the hash table
ArrayList akeys = new ArrayList (ht. Keys); // do not forget to import System. Collections
Akeys. Sort (); // Sort by letter
Foreach (string skey in akeys)
{
MessageBox. Show (skey + ":");
MessageBox. Show (ht [skey]. ToString (); // output after sorting
}
ArrayList usage
Private static void AddToList (ArrayList list, string p)
{
If (list. Contains (p) = false)
List. Add (p );
}

Private void button#click (object sender, EventArgs e)
{
ArrayList list = new ArrayList ();
AddToList (list, "Table1 ");
AddToList (list, "Table4 ");
AddToList (list, "Table1 ");
AddToList (list, "Table3 ");
AddToList (list, "Table2 ");
AddToList (list, "Table2 ");

Foreach (string s in list)
{
MessageBox. Show (s );
}
}
List
List <string> listStr = new List <string> ();
ListStr. Add ("123 ");
ListStr. Add ("456 ");
ListStr. Add ("789 ");
MessageBox. Show (listStr [2]); // "789"

Dictionary
Advantages of generics (C # programming guide)
In C #, the typical schema has an uncommon Dictionary set besides the familiar IList and HashTable.
In comparison, Dictionary has the best performance and is also a lightweight collection. The efficiency is higher than that of HashTable. The main reason is that Dictionary supports strong type declarations.
In earlier versions of the Public Language Runtime Library and C # language, generalization is achieved through forced conversion between types and general basic types of objects, the generic model provides a solution to this limitation. By creating a generic class, you can create a set of secure types during compilation.
Any reference or value type added to the ArrayList is implicitly forcibly converted to an Object. If the item is of the value type, you must perform the packing operation when adding it to the list, and cancel the packing operation during retrieval. Both forced conversion and packing and unboxing reduce the performance. When circular access is required for large sets, the effect of packing and unboxing is significant.
Compared with ArrayList, the unique syntax added when using List <T> is Declaration and type parameters in instantiation. Although this slightly increases the encoding complexity, you can create a list that is safer and faster than ArrayList, especially when list items are of the value type.
The Dictionary generic class provides a ing from a group of keys to a group of values. Each added item in the dictionary is composed of a value and its associated keys. Key-based value retrieval is very fast, close to O (1), because the Dictionary class is implemented as a hash table.
1,
Dictionary <int, string> fruit = new Dictionary <int, string> ();
// Adding the duplicate key will cause an exception.
Fruit. Add (1, "apple ");
Fruit. Add (2, "orange ");
Fruit. Add (3, "banana ");
Fruit. Add (4, "pineapple ");

// Because generics are introduced, Object-to-int conversion is not required after the key is taken out, and the set of values is the same.
Foreach (int I in fruit. Keys)
{
MessageBox. Show ("key:" + I. ToString () + "value:" + fruit);
}
2,
Dictionary <string, string> fruit = new Dictionary <string, string> ();

// Adding the duplicate key will cause an exception.
Fruit. Add ("1", "apple ");
Fruit. Add ("2", "orange ");
Fruit. Add ("3", "banana ");
Fruit. Add ("4", "pineapple ");

// Because generics are introduced, Object-to-int conversion is not required after the key is taken out, and the set of values is the same.
Foreach (string I in fruit. Keys)
{
MessageBox. Show ("key:" + I. ToString () + "value:" + fruit);
}

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.