Learn About ArrayList in C # (Part 2)

Before reading this article, please go through the following article:

  1. Learn about ArrayList in C#: Part 1

1. Two browsing methods ArrayList

In the following examples we explore two methods to browse an ArrayList. 

In the previous section we will became familiar with how to use a foreach loop and var keyword combinations. Here we will use a special method to navigate in the loop : ( ;;). As we know each ArrayList that is created has a Count property that gives us the number of elements in the list, so we must specify the elements of the number of iterations of the for loop for (;;). The next issue becomes, taking the value of each element in the list. After that, we will use the Index to retrieve the element. Note: The index runs from 0 -> count-1.

Both methods return the same results but we can see that the foreach () var keyword combination is much simpler, and that clearly this way we can also save the values back. 

001-arraylist-2-microsofttech.net.jpg


2. Sort list

In the System.collection library, a supporting method (method) Sort (); is used to sort an ArrayList. Sorting in ascending order can be applied to many different types of data. If you want to sort in descending order, you may combine it with the Reverse method. 

002-arraylist-2-microsofttech.net.jpg

3. Add or remove elements from the List

To add or remove one element in the list you can use the Insert and RemoveAt methods. With the Insert method the first argument is the index (position) that the new element is inserted and the second argument is the value of that element. As for the RemoveAt method, it should include the location of the element to delete.

003-arraylist-2-microsofttech.net.jpg

From My Site: http://microsofttech.net/lap-trinh/tim-hieu-ve-arraylist-trong-c-p-2.html


Similar Articles