System.Collections.ArrayList

Added in .NET 1.1
This allows you to work with a set of values as if they were an array and a collection at the same time.

System.Collections.ArrayList myArrayList; 

Represents a collection of items whose size can be changed dynamically.


ArrayList vs Array

An arraylist is a combination of an array and a collection.
Imagine that you need to keep track of various objects but you have no idea how many.
It is difficult to use an array for this purpose because you must declare the size of the array at compile time.
This is an aray whose size is dynamically increased as required.


Features

An arraylist has the following functionality:
You can address a value by its index
You can quickly sort and reverse the list
You can search sequentially
You can append elements to the end and insert them in a particular position


Different Data Types

These items can have different data types.

System.Collections.ArrayList myArrayList; 
myArrayList = new System.Collections.ArrayList();
myArrayList.Add("string");
myArrayList.Add(20);
myArrayList.Add(false);
Console.WriteLine(myArrayList[1]);


Do not return null ?

return new System.Collections.ArrayList 

Methods

AddAppends an element after the last element in the collection and returns the index where it was inserted.
AddRange 
BinarySearch 
ClearRemoves all elements from the collection.
Clone 
ContainsReturns TRUE if an element exists in the collection.
CopyToCopies the elements from the collection to a new one-dimensional array, starting at the specified index in the array.
FixedSize 
GetEnumeratorReturns an enumerator to iterate an ArrayList
GetRange 
IndexOfReturns the index of the element in the collection, or -1 if it cannot be found.
InsertInserts an element at the specified index.
InsertRange 
LastIndexOf 
ReadOnly 
RemoveRemoves an element from the collection
RemoveAtRemoves an element at the specified index from the collection
RemoveRange 
Repeat 
ReverseReverses the order of the elements
SetRange 
SortAlphabetically sorts the elements
ToArrayCopies all the elements to a new one-dimensional array
TrimToSize 

Properties

CapacityProperty contains the number of elements the srray can currently hold.
CountReturns the number of elements in the collection.
IsFixedSizeReturns TRUE if no more elements can be added to the collection.
IsReadOnlyReturns TRUE is the elements cannot be altered.

Generic Version

The generic version of this collection is System.Collections.Generic.List


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext