4 common collections in C #

Source: Internet
Author: User

1. ArrayList

ArrayList is similar to an array, which is also called an array list. ArrayList can be dynamically maintained, while the array capacity is fixed.

Its indexes will be re-allocated and adjusted based on program expansion. Like an array, the data it stores is called an element, and the number of elements it stores is its capacity. The default initial capacity is 0. When using it, you must introduce the namespace System. Connections. The following Code defines an ArrayList:

Using System. Collections;

// Create an ArrayList object with a capacity of 0

ArrayList myList = new ArrayList ();

// Create an ArrayList object with a capacity of 5

ArrayList myList = new ArrayList ();

// Obtain the actual number of elements contained in the object

Int num = myList. Count ();

ArrayList adds an element through the Add () method. The method returns an Int value, which indicates the index of the added element in the set.

Parameter: If the elements added to the ArrayList are of the value type, these elements are automatically converted to the Object reference type and saved. Therefore, all elements in the ArrayList are references to objects.

There are three methods to delete the elements in the ArrayList:

Object Name. RomoveAt (int index );

Object Name. Romove (Object value );

Object Name. Clear (); (this method will delete all elements in the Set, commonly known as "empty "~~~)


2. HashTable

C #/provides a data structure called HashTable, which is usually called a hash table. Some people call it a "Dictionary ". hashTable data is organized by Key and Value. Like ArrayList, HashTable data also belongs to System. in the Collections namespace, each element it stores is a key/value pair. the following are common methods and attributes of HashTable:


Attribute name: Count Description: gets the number of key/value pairs contained in HashTable.

Attribute name: Keys Description: gets the set that contains the key words in HashTable.

Attribute name: Values Description: obtains a set of Values contained in HashTable.


Method Name: Add (Object key, Object Value) Description: adds elements with specified keys and values to HashTable.

Method Name: Remove (Object Key) Description: Removes elements with specific keys from HashTable.

Method Name: Clear () Description: removes all elements from HashTable.

Unlike ArrayList, you can directly obtain the specific value by using the key name when accessing the HashTable element. Similarly, because the value type is Object. so when you get a value, you also need to get the specified type of object through type conversion.


3. Generic set: List

Before introducing generic collections, we need to understand what generics are:

Generic is a new feature in C #2.0. Generics introduce a new concept: type parameters. The type parameter (T) is used to reduce the risk of forced conversion to packing during running. You can use generic collections to maximize code reuse, protect type security, and improve performance.

Define a List The generic set method is as follows:

List Object Name = new List ();

List Adding, retrieving, deleting, and traversing elements are similar to ArrayList, This ensures the type security. You do not need to convert the data type when getting the element. Compared with ArrayList

Not required: List Type constraints are imposed on the stored elements, while ArrayList can add any types. Add and read value type element List ArrayList does not need to be split and packed, but ArrayList does need to be split and packed.

Similarities: access the elements in the set through indexes. The methods for adding and deleting elements are the same.


4. Generic set Dictionary

It has all the features of generics. It checks the type constraints during compilation and does not require type conversion when getting elements, and it stores data in a similar way as HashTable. It is also saved through Key/Value pairs. Syntax:

Dictionary Object Name = new Dictionary

In, K indicates the type of the Key in the Set, and V indicates the type of Value. Its meaning and List Is the same. For example:

Dictionary Engineers = new Dictionary ();

In this set, the Key type is string type, and the Value type is SE type. Next we will set Dictionary Compared with HashTable:

Difference: Dictionary Type constraints are imposed on the stored elements, while HashTable can add any types. Dictionary Adding and reading value type elements do not require unpacking or packing, whereas HashTable requires unpacking and packing.

Similarities: Get the Value using the Key. The methods for adding, deleting, and traversing elements are the same.



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.