Net-informations.com

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-sharp-arraylist

C# ArrayList examples

  1. Add an Item in an ArrayList
  2. Insert an Item in a specified position in an ArrayList
  3. Remove an Item from ArrayList
  4. Remove an item from a specified position
  5. Remove a range of elements from the ArrayList
  6. Sort Items in an ArrayList
  7. Count and Capacity Properties
  8. Convert an ArrayList to Array
  9. When to use array[] over ArrayList

How to add an Item in an ArrayList ?

Syntax

  1. 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

  1. index : The position of the item in an ArrayList
  2. 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

  1. 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

  1. 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.










net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About