Comparison of common collection classes in C #

Source: Internet
Author: User

First, non-generic sets and generic collections

Non-generic collection: Array, ArrayList, HashTable, Queue, Statck, SortedList

Generic collection: List, Dictionary, Queue, Stack, SortedList

Ii. Common collection Classes

Array, ArrayList, List

(a) Array:

That is, a common array form, fixed size, and a namespace of system

(b) ArrayList:

The namespace is system.collection, which is a complex version of the array. The ArrayList class provides some of the features that are available in most collections classes, but are not available in the Array class.

Variable length, using the Add () method can be used to dynamically delete the ArrayList collection operation.

The ArrayList element type belongs to the object type, because it does not point out the exact type, so when the data is stored or retrieved, the operation of boxing and unpacking will occur, causing the system to consume.

Use:

ArrayList arrayList1 = new ArrayList ();
Arraylist1.add ("a");
Arraylist1.add (1);
Arraylist1.add ("B");
Response.Write (Arraylist1[1]);

ArrayList-owned methods:

1:add () Adds an element to the array,
2:remove () to delete an element in an array
3:removeat (int i) delete 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 order from small to large
6:clone () copy an array

(c) List

The so-called generic collection, the namespace isSystem.Collections.Generic;使用方法与ArrayList类似,只是给出了具体的类型T,使用时无装箱拆箱操作,节约了内存

Usage:

list<string> names = new list<string> ();
Names. ADD ("Qiao");
Names. Add ("Yin");
Names. ADD ("Wasp");
Traverse List
foreach (string name in Names)
{
Console.WriteLine (name);
}
Inserting an element into the list
Names. Insert (2, "Zhang Sanfeng");
Removing the specified element
Names. Remove ("Wasp");

Comparison of common collection classes in C #

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.