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
-
- Public interface ilist <t>: icollection <t>, ienumerable <t> ienumerable
-
- {
-
- T item;
-
- Abstract int indexof (T item );
-
- Abstract void insert (INT index, t item );
-
- Abstract void removeat (INT index );
-
- }
- Public interface ilist: icollectionienumerable
-
- {
-
- Bool isfixedsize;
-
- Bool isreadonly;
-
- Object item;
-
- Abstract int add (object value );
-
- Abstract void clear ();
-
- Abstract bool contains (object value );
-
- Abstract int indexof (object value );
- Abstract void insert (INT index, object value );
-
- Abstract void remove (object value );
-
- Abstract void removeat (INT index );
-
- }
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
- Public override bool equals (Object OBJ)
- {
- Testtype tType = OBJAs testtype;
- If (tType = NULL)
- {
- Return false;
- }
- // Other ServicesCode
- ...
- }
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