快捷搜索:

How to use C# ArrayList Class

How to use C# ArrayList Class

ArrayList is one of the most flexible data structure from CSharp Collections. ArrayList contains a simple list of values. ArrayList implements the IList interface using an array and very easily we can add , insert , delete , view etc. It is very flexible because we can add without any size information , that is it will grow dynamically and also shrink.

C# ArrayList examples

Add an Item in an ArrayList

Insert an Item in a specified position in an ArrayList

Remove an Item from ArrayList

Remove an item from a specified position

Remove a range of elements from the ArrayList

Sort Items in an ArrayList

Count and Capacity Properties

Convert an ArrayList to Array

When to use array[] over ArrayList

How to add an Item in an ArrayList ? Syntax

object : The Item to be add the ArrayList

The Add() method of ArrayList is used to add new items in ArrayList.

example output

How to add elements in C# arraylist


How to Insert an Item in an ArrayList ?

Elements can be added or removed from the C# ArrayList collection at any point in time. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayList at the specified index.

Syntax

index : The position of the item in an ArrayList

object : The Item to be add the ArrayList

example output

How to insert c# arraylist elements


How to remove an item from arrayList ?

Arraylist remove() method removes the first occurrence of a specific object from the ArrayList.

Syntax

object : The Item to be add the ArrayList

example output

How to remove an item from C# arraylist


Remove an item in a specified position from an ArrayList ?

Arraylist ArrayList.RemoveAt(index) remove the specified index element from arraylist.

Syntax

index : the position of an item to remove from an ArrayList

example output

how to remove an item from a specified index C# arralist


Arraylist RemoveRange

ArrayList RemoveRange(Int32, Int32) method is used to remove a range of elements from the ArrayList. The following program removes the range 1 to 2 elements from Arraylist

example output

how to remove range of items from c# arraylist


How to sort ArrayList ?

Arraylist Sort() method sorts the elements in the entire ArrayList.

Syntax example output

How to sort C# arraylist


Arraylist Count and Capacity

The Count property returns the number of elements in an ArrayList, which is a non-generic collection that stores its length in a property called Count.

The Capacity property returns the number of elements that the ArrayList can store. Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.

example output

C# Arraylist count property C# arraylist Capacity property


Convert an ArrayList to Array

ArrayList ToArray() method return an Object array containing copies of the elements of the ArrayList. The ToArray() method copy the elements of the ArrayList to a new Object array.

Syntax example output

C# Arraylist to C# array


When to use array[] over ArrayList

C# Arrays are strongly typed , and work well as parameters. If you know the length of your collection and it is fixed, it is better to use an array.


Next :  How to use C# HashTable Class

您可能还会对下面的文章感兴趣: