C # generic learning 03 -- replace ArrayList with the corresponding generic version

Source: Internet
Author: User

1. A simple example of using the System. Collection. ArrayList object:
View Code
Public static void UseNonGenericArrayList ()
{
// Create an ArrayList.
ArrayList numbers = new ArrayList ();
Numbers. Add (1); // causes the boxing operation
Numbers. Add (2); // causes the boxing operation
// Display All integers in the ArrayList
// Each iteration results in The binning operation
Foreach (int I in numbers)
{
Console. WriteLine (I );
}
Numbers. Clear ();
}

II. A simple example of using the System. Collections. Generic. List object:
View Code
Public static void UseGenericList ()
{
// Create a List.
List <int> numbers = new List <int> ();
Numbers. Add (1 );
Numbers. Add (2 );
// Display All integers in the List.
Foreach (int I in numbers)
{
Console. WriteLine (I );
}
Numbers. Clear ();
}

Because almost all applications use ArrayList, it is a good choice to improve the execution efficiency of your applications. This alternative is very easy for applications that simply use ArrayList. Note that, for example, the generic List class does not implement the Icloneable interface, while the ArrayList class implements it.
 
Figure 1 shows equivalent members of two classes

 

 

The members of several arraylists in Figure 1 do not have one-to-one correspondence with those of the generic List. From the perspective of attributes, only the Capacity, Count, and Item attributes exist. To make up for several missing attributes in the List class, you can explicitly convert them to the Ilist interface. The following code demonstrates how to use these Explicit conversions to obtain missing attributes.
 
List <int> numbers = new List <int> ();
Console. WriteLine (IList) numbers). IsReadOnly );
Console. WriteLine (IList) numbers). IsFixedSize );
Console. WriteLine (IList) numbers). IsSynchronized );
Console. WriteLine (IList) numbers). SyncRoot );

 
Note: The IsFixedSize and IsSynchronized attributes always return false because of the lack of generic List code that returns the synchronous version and the lack of generic List code that returns a fixed size. When the SyncRoot attribute is called, the same object is always returned. Essentially, this attribute returns the this pointer. Microsoft has decided to remove the function of creating synchronization members from all generic collection classes. Instead, we recommend that you use the lock keyword to lock the entire set or other types of synchronization objects to meet your needs.
Static ArrayList. Repeat does not have corresponding methods in generic List. Instead, you can use the following generic method:
 
View Code
Public static void Repeat <T> (List <T> list, T obj, int count)
{
If (count <0)
{
Throw (new ArgumentException (
"The count parameter must be greater than or equal to zero "));
}
For (int index = 0; index <count; index ++)
{
List. Add (obj );
}
}

This generic method has three parameters:
List generic List object
Obj will be added to the generic List with a specified number of times
Count: number of times that obj is added to a generic class
Because the Clone method does not appear in the generic List class (because this class does not implement the Icloneable Interface), you can use the GetRange method of the generic List class as an alternative.
List <int> oldList = new List <int> ();
// Add an element to oldList...
List <int> newList = oldList. GetRange (0, oldList. Count );

The GetRange method performs a shortest copy (similar to the Clone method in ArrayList) on elements in a range in the List object ). In this example, the range is all elements.
Tip: the default initial capacity of ArrayList is 16 elements, and the default initial capacity of List <T> is 4 elements. This means that when 17th elements are added, the List <T> has to change the size (re-allocate memory) three times, and the ArrayList only needs to be re-allocated once. This must be taken into account when evaluating application procedural capabilities.

 

From chen3jian

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.