What is the difference between dictionary, ArrayList, Hashtable and Array in C # __c#

Source: Internet
Author: User

C # Collection Class Array Arraylist List Hashtable Dictionary Stack Queue
1. The array is fixed in size and cannot be scaled. Although System.Array.Resize this generic method can reset the array size,
However, the method is to recreate the new set size array with the elements of the old array initialized. Then the previous array is discarded.
And the collection is variable-length.
2. Array to declare the type of the element, the element type of the collection class is object.
3. Array readable and writable can not declare only reading group. Collection classes can provide readonly methods to use collections in read-only mode.
4. An array must have an integer subscript to access a particular element, yet many times such subscripts are not very useful.
The collection is also a list of data without using subscript access.
Many times the collection has a custom subscript type, and for queues and stacks there is no support for subscript access at all.

Array
Int[] IntArray1;
Initializing a declared one-dimensional array
IntArray1 = new Int[3];
IntArray1 = new int[3]{1,2,3};
IntArray1 = new int[]{1,2,3};

The ArrayList class object is designed to be a dynamic array type, and its capacity expands appropriately as needed
Method
1:add () Adds an element to the array,
2:remove () deletes an element in an array
3:removeat (int i) deletes an element with an index value of I in an array
4:reverse () Reverses the elements of an array
5:sort () The elements of an array in a small to large order
6:clone () copy an array

List
A strongly typed list of objects that can be accessed through the index. Provides methods for searching, sorting, and manipulating lists
When deciding whether to use the list or the ArrayList class (both have similar functionality), remember that the list class is executed in most cases
Better and is of type safety. If you use a reference type for the type T of the List class, the

The behavior of two classes is exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.

If a value type is used for type T, the compiler will specifically generate the implementation of the List class for that value type.
This means that the element can be used without boxing the list element of the lists object, and that you are creating about 500 lists

element, the memory that does not save the list element boxing will be greater than the memory used to generate the class implementation.

Dictionary
Represents a collection of keys and values. The order in which the dictionary traverses the output is the sequence of joins, which differs from the Hashtable

SortedList class
Similar to a hash table, the difference is that the key array in the SortedList is sorted in order

Hashtable class
Hash table, name-value pair. Similar to dictionaries (more powerful than arrays). The hash table is optimized, and the objects that access the subscript are hashed out first.
If you access the element in any type of key value faster than the other collection.
The GetHashCode () method returns an int that uses the value of this key to generate the int data.
Hash table gets this value finally returns an index representing the location of the data item with the given hash stored in the dictionary.

Stack class
Stack, LIFO first. Push method into Stack, pop method out stack.

Queue class
Queue, advanced first out. Enqueue method into the queue, dequeue the method out of the queue.

Dictionary
System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry ("Key1", "value1");

Dictionary fruit = new Dictionary ();

Adding a duplicate key throws an exception
Fruit. ADD (1, "Apple");
Fruit. ADD (2, "orange");
Fruit. ADD (3, "banana");
Fruit. ADD (4, "pineapple");

Because of the introduction of generics, the key is removed without the need for an object to int conversion, the same set of values
foreach (int i in fruit. Keys)
{
Console.WriteLine ("Key is: {0} value is: {1}", i,fruit);
}
Deletes the specified key, the value
Fruit. Remove (1);
Determines whether the specified key is included
if (fruit. ContainsKey (1))
{
Console.WriteLine ("Include this Key");
}
Clear all objects in the collection
Fruit. Clear ();
}

ArrayList
System.Collections.ArrayList list=new System.Collections.ArrayList ();
List. ADD (1);
List. ADD (2);
for (int i=0;i
{
System.Console.WriteLine (List[i]);
}

List
Declares a list object, adding only the string argument
List names = new list ();
Names. ADD ("Joe");
Names. Add ("Yin");
Names. ADD ("Wasp");
Traverse List
foreach (string name in Names)
{
Console.WriteLine (name);
}
Inserting elements into the list
Names. Insert (2, "Zhang Sanfeng");
To remove a specified element
Names. Remove ("Wasp");

HashTable
System.Collections.Hashtable table=new System.Collections.Hashtable ();
Table. ADD ("table1", 1);
Table. ADD ("Table2", 2);
System.Collections.IDictionaryEnumerator d=table. GetEnumerator ();
while (D.movenext ())
{
System.Console.WriteLine (D.entry.key);
}

Queue
System.Collections.Queue queue=new System.Collections.Queue ();
Queue. Enqueue (1);
Queue. Enqueue (2);

System.Console.WriteLine (queue. Peek ());
while (queue. COUNT>0)
{
System.Console.WriteLine (queue. Dequeue ());
}

SortedList
System.Collections.SortedList list=new System.Collections.SortedList ();
List. ADD ("Key2", 2);
List. ADD ("Key1", 1);
for (int i=0;i
{
System.Console.WriteLine (list. Getkey (i));
}

Stack
System.Collections.Stack stack=new System.Collections.Stack ();
Stack. Push (1);
Stack. Push (2);

System.Console.WriteLine (Stack. Peek ());
while (stack. COUNT>0)
{
System.Console.WriteLine (Stack. Pop ());
}

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.