Differences between collection, list, And arraylist in C #

Source: Internet
Author: User
Http://blog.csdn.net/cronzb/article/details/6429241

 


In C #. NET 2.0 library, the collection class is in the system, system. collections, system. collections. generic and system. collections. classes in the objectmodel namespace, including collection, keyedcollection, readonlycollection, list, array, stack, queue, and arraylist.

 

The following are the differences between the collection <t>, list <t> and arraylist classes.

1. List is used for classes in high-performance environments, and collection is used to expand

With collection, developers can override clearitems, insertitem, removeitem, and setitem because they are of the protected virtual type, but list <t> does not have these extensions.

 

2. Different interfaces are implemented.

Collection <t> implement ilist <t>, icollection <t>, ienumerable <t>, ilist, icollectionienumerable

List <t> implement ilist <t>, icollection <t>, ienumerable <t>, ilist, icollectionienumerable

Arraylist implements ilist, icollection, ienumerable, and icloneable

Ilist <t>, icollection <t>, ienumerable <t> and ilist, icollection, and ienumerable are completely different. The former is used for the model,

View plain
  1. Public interface ilist <t>: icollection <t>, ienumerable <t> ienumerable
  2. {
  3. T item;
  4. Abstract int indexof (T item );
  5. Abstract void insert (INT index, t item );
  6. Abstract void removeat (INT index );
  7. }
  8. Public interface ilist: icollectionienumerable
  9. {
  10. Bool isfixedsize;
  11. Bool isreadonly;
  12. Object item;
  13. Abstract int add (object value );
  14. Abstract void clear ();
  15. Abstract bool contains (object value );
  16. Abstract int indexof (object value );
  17. Abstract void insert (INT index, object value );
  18. Abstract void remove (object value );
  19. Abstract void removeat (INT index );
  20. }

 

On the other hand, collection <t> and list <t> also implement ilist and icollectionienumerable, which shows that arraylist provides more methods.

 

3. Differences between fan type and non-fan type

Arraylist is a non-fan class. In this way, this set can contain different types of members. We can see that the add method is add (Object OBJ), so this is a miscellaneous class of objects. When using this class for operations, indexof, remove, and so on all use the equals and hashcode of the class. Therefore, if it is a self-implemented class, you must determine whether it is of the same type.

 

For example, this class is testtype.

 

View plain
    1. Public override bool equals (Object OBJ)
    2. {
    3. Testtype tType = OBJAs testtype;
    4. If (tType = NULL)
    5. {
    6. Return false;
    7. }
    8. // Other ServicesCode
    9. ...
    10. }

 

 

Summary:

If you have expansion requirements, you can consider using collection. If you have performance requirements, consider using list. If you want to store different types of objects, use arraylist.

 

This article references the. NET 2.0 class library and http://blogs.msdn.com/ B /codeanalysis/archive/2006/04/27/585476.aspx

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.