C# ArrayList

The ArrayList Collection implements the IList Interface. The size of an ArrayList in C# can be modified i.e., you can easily add or remove elements. This is not the case with Arrays in C#. Once created, you cannot modify the C# Arrays. ArrayList can have duplicate elements. The ArrayList class is defined in the System.Collections namespace:

Create an ArrayList

Let us see how to create an ArrayList in C#. Use the new keyword to create a new ArrayList:

Let us see how to create an ArrayList with integer elements:

Output

Count the number of ArrayList elements

To count the number of elements an ArrayList i.e., how many elements it has, use the count property. Let us see an example:

Output

Insert Elements in an ArrayList

To insert elements at a specific index in an ArrayList, use the Insert() method. Under the parameter, set the index number where the element needs to be inserted and the element itself:

Above, we are inserting an element 100 at index 2.

Let us see an example of inserting elements in an ArrayList:

Output

Remove an Element in an ArrayList

To remove an element from the ArrayList, use the Remove() method and mention the element to be removed as a parameter:

Output

Sort an ArrayList

To sort an ArrayList, use the Sort() method in C#. Let us see an example:

Output

Fetch an ArrayList element

To fetch an element at a specific position in an ArrayList, use the index of the element. Let us see an example:

Output

Check for the existence of an ArrayList element

Use the contains() method to check for the existence of an element in C#. Let us see an example:

Output


Read More

C# - Installation and Setup
C# vs Java - Difference
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading