The ArrayList Collection implements the IList Interface and uses an array that is dynamic. The array resized automatically as required and dynamic memory allocation is allowed. The ArrayList Collection is an alternative of arrays.
The different constructors and their description is given as follows:
Table: Constructors in ArrayList in C#
Source: MSDN
Constructors | Description |
---|---|
ArrayList() | This constructor initializes a new instance of the ArrayList class that is empty and has the default initial capacity. |
ArrayList(ICollection) | This constructor initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied |
ArrayList(Int32) | This constructor initializes a new instance of the ArrayList class that is empty and has the specified initial capacity. |
The different properties and their description is given as follows:
Table: Properties in ArrayList in C#
Source: MSDN
Properties | Description |
---|---|
Capacity | This property gets or sets the number of elements that the ArrayList can contain. |
Count | This property gets the number of elements actually contained in the ArrayList. |
IsFixedSize | This property gets a value indicating whether the ArrayList has a fixed size. |
IsReadOnly | This property gets a value indicating whether the ArrayList is read-only. |
IsSynchronized | This property gets a value indicating whether access to the ArrayList is synchronized (thread safe). |
Item[Int32] | This property gets or sets the element at the specified index. |
SyncRoot | This property gets an object that can be used to synchronize access to the ArrayList. |
The different methods and their description is given as follows:
Table: Methods in ArrayList in C#
Source: MSDN
Methods | Description |
---|---|
Adapter(IList) | This method creates an ArrayList wrapper for a specific IList. |
Add(Object) | This method adds an object to the end of the ArrayList. |
BinarySearch(Object) | This method searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element. |
BinarySearch(Object, IComparer) | This method searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element. |
Clear() | This method removes all elements from the ArrayList. |
Clone() | This method creates a shallow copy of the ArrayList. |
Contains(Object) | This method determines whether an element is in the ArrayList. |
CopyTo(Array) | This method copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. |
CopyTo(Array, Int32) | This method copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array. |
Equals(Object) | This method determines whether the specified object is equal to the current object. (Inherited from Object) |
FixedSize(ArrayList) | This method returns an ArrayList wrapper with a fixed size. |
FixedSize(IList) | This method returns an IList wrapper with a fixed size. |
GetEnumerator() | This method returns an enumerator for the entire ArrayList. |
GetEnumerator(Int32, Int32) | This method returns an enumerator for a range of elements in the ArrayList. |
GetHashCode() | This method serves as the default hash function. |
GetRange(Int32, Int32) | This method returns an ArrayList which represents a subset of the elements in the source ArrayList. |
GetType() | This method gets the Type of the current instance. |
IndexOf(Object) | This method searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList. |
IndexOf(Object, Int32) | This method searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element. |
IndexOf(Object, Int32, Int32) | This method searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements. |
Insert(Int32, Object) | This method inserts an element into the ArrayList at the specified index. |
ReadOnly(ArrayList) | This method returns a read-only ArrayList wrapper. |
ReadOnly(IList) | This method returns a read-only IList wrapper. |
Remove(Object) | This method removes the first occurrence of a specific object from the ArrayList. |
Repeat(Object, Int32) | This method returns an ArrayList whose elements are copies of the specified value. |
Reverse() | This method reverses the order of the elements in the entire ArrayList. |
Sort() | This method sorts the elements in the entire ArrayList. |
Sort(IComparer) | This method sorts the elements in the entire ArrayList using the specified comparer. |
Sort(Int32, Int32, IComparer) | This method sorts the elements in a range of elements in ArrayList using the specified comparer. |
Synchronized(ArrayList) | This method returns an ArrayList wrapper that is synchronized (thread safe). |
ToArray() | This method copies the elements of the ArrayList to a new Object array. |
ToArray(Type) | This method copies the elements of the ArrayList to a new array of the specified element type. |
ToString() | This method returns a string that represents the current object. |
TrimToSize() | This method sets the capacity to the actual number of elements in the ArrayList. |
Some of the SortedList operations are given as follows:
Elements can be added to the ArrayList using the Add() method. It adds the specified values to the ArrayList. The program that demonstrates this is given as follows:
Source Code: Program to add elements in ArrayList in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(5); arr.Add(1); arr.Add(9); arr.Add(6); arr.Add(2); arr.Add(8); arr.Add(3); arr.Add(7); Console.Write("ArrayList elements are: "); foreach (int i in arr) { Console.Write(i + " "); } } } }
The output of the above program is given below:
ArrayList elements are: 5 1 9 6 2 8 3 7
An element can be deleted from the ArrayList using the Remove() method. This method deletes the first occurance of the value that is provided. The program that demonstrates this is given as follows:
Source Code: Program to delete elements from ArrayList in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(5); arr.Add(1); arr.Add(9); arr.Add(6); arr.Add(2); arr.Add(8); arr.Add(3); arr.Add(7); Console.Write("Original ArrayList elements: "); foreach (int i in arr) { Console.Write(i + " "); } arr.Remove(1); arr.Remove(8); arr.Remove(5); Console.WriteLine(); Console.Write("ArrayList elements after deletion: "); foreach (int i in arr) { Console.Write(i + " "); } } } }
The output of the above program is given below:
Original ArrayList elements: 5 1 9 6 2 8 3 7 ArrayList elements after deletion: 9 6 2 3 7
The Reverse() method is used to reverse the order of the ArrayList elements. The program that demonstrates this is given as follows:
Source Code: Program to reverse elements in ArrayList in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(5); arr.Add(1); arr.Add(9); arr.Add(6); arr.Add(2); arr.Add(8); arr.Add(3); arr.Add(7); Console.Write("ArrayList elements: "); foreach (int i in arr) { Console.Write(i + " "); } // reversing array arr.Reverse(); Console.WriteLine(); Console.Write("Reversed ArrayList elements: "); foreach (int i in arr) { Console.Write(i + " "); } } } }
The output of the above program is given below:
ArrayList elements: 5 1 9 6 2 8 3 7 Reversed ArrayList elements: 7 3 8 2 6 9 1 5
The Sort() method is used to sort the ArrayList elements. The program that demonstrates this is given as follows:
Source Code: Program to sort elements in ArrayList in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(5); arr.Add(1); arr.Add(9); arr.Add(6); arr.Add(2); arr.Add(8); arr.Add(3); arr.Add(7); Console.Write("ArrayList elements: "); foreach (int i in arr) { Console.Write(i + " "); } arr.Sort(); Console.WriteLine(); Console.Write("Sorted ArrayList elements: "); foreach (int i in arr) { Console.Write(i + " "); } } } }
The output of the above program is given below:
ArrayList elements: 5 1 9 6 2 8 3 7 Sorted ArrayList elements: 1 2 3 5 6 7 8 9
To convert an ArrayList to Array, the following is the code:
Source Code: Program to convert an ArrayList to Array in C#
using System; using System.Collections; public class Example { public static void Main() { ArrayList arr = new ArrayList(); arr.Add("AB"); arr.Add("BC"); arr.Add("DE"); arr.Add("FG"); arr.Add("HI"); arr.Add("JK"); string[] myArr = arr.ToArray(typeof(string)) as string[]; foreach (string a in myArr) { Console.WriteLine(a); } } }
The above program gives the following output:
AB BC DE FG HI JK
At times, you may want to delete an element at a particular position in an ArrayList. For that, use the RemoveAt() method:
Source Code: Program to delete an element at a particular position in an ArrayList in C#
using System; using System.Collections.Generic; public class Example { public static void Main(string[] args) { var l = new List<string>(); l.Add("One"); l.Add("Two"); l.Add("Three"); foreach (var arr in l) { Console.WriteLine(arr); } l.RemoveAt(1); Console.WriteLine("New list after deleting a single element:"); foreach (var arr in l) { Console.WriteLine(arr); } } }
The above program gives the following output:
One Two Three New list after deleting a single element: One Three
Use the IsFixedSize() property in C# to get a value stating whether the ArrayList has a fixed size or not
Let us see an example:
Source Code: Program to check whether the ArrayList has a fixed size or not in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(100); arr.Add(120); arr.Add(90); arr.Add(60); arr.Add(200); arr.Add(110); arr.Add(130); arr.Add(180); arr.Add(195); arr.Add(165); arr.Add(80); Console.Write("ArrayList elements = "); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine("\nIsFixedSize? " + arr.IsFixedSize); } } }
The above program gives the following output:
ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 IsFixedSize? False
Use the IsFixedSize() property in C# to get a value stating whether the ArrayList is read only or not
Let us see an example:
Source Code: Program to check whether the ArrayList is read only or not in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(5); arr.Add(1); arr.Add(9); arr.Add(6); arr.Add(2); arr.Add(8); arr.Add(3); arr.Add(7); Console.Write("ArrayList elements = "); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine("\nIsReadOnly? " + arr.IsReadOnly); } } }
The above program gives the following output:
ArrayList elements = 5 1 9 6 2 8 3 7 IsReadOnly? False
Use the Capacity property to get the maximum size of the ArrayList. Let us now see an example:
Source Code: Program to get the maximum size of the ArrayList in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(100); arr.Add(120); arr.Add(90); arr.Add(60); arr.Add(200); arr.Add(110); arr.Add(130); arr.Add(180); arr.Add(195); arr.Add(165); arr.Add(80); Console.Write("ArrayList elements = "); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine("\nCapacity = " + arr.Capacity); } } }
The above program gives the following output:
ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 Capacity = 16
Use the Count property to count the number of elements in a ArrayList . Let us see an example:
Source Code: Program to count the number of elements in the ArrayList in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(100); arr.Add(120); arr.Add(90); arr.Add(60); arr.Add(200); arr.Add(110); arr.Add(130); arr.Add(180); arr.Add(195); arr.Add(165); arr.Add(80); arr.Add(99); arr.Add(145); arr.Add(198); arr.Add(210); Console.Write("ArrayList elements = "); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine("\n\nCount = " + arr.Count); Console.WriteLine("Capacity = " + arr.Capacity); } } }
The above program gives the following output:
ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 99 145 198 210 Count = 15 Capacity = 16
At times, you may need to fetch an element at a specified position in ArrayList. For that, the following is the code:
Source Code: Program to fetch an element at a specified position in ArrayList in C#
using System; using System.Collections; namespace ArrayListDemo { class Example { static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(100); arr.Add(120); arr.Add(90); arr.Add(60); arr.Add(200); arr.Add(110); arr.Add(130); arr.Add(180); arr.Add(195); arr.Add(165); arr.Add(80); arr.Add(99); arr.Add(145); arr.Add(198); arr.Add(210); Console.Write("ArrayList elements = "); foreach (int i in arr) { Console.Write(i + " "); } Console.WriteLine("\n\nCount = " + arr.Count); Console.WriteLine("Capacity = " + arr.Capacity); Console.WriteLine("Element {0} is \"{1}\"", 2+"th", arr[1]); Console.WriteLine("Element {0} is \"{1}\"", 4+"th", arr[3]); Console.WriteLine("Element {0} is \"{1}\"", 6+"th", arr[5]); Console.WriteLine("Element {0} is \"{1}\"", 8+"th", arr[7]); } } }
The above program generates the following output:
ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 99 145 198 210 Count = 15 Capacity = 16 Element 2th is "120" Element 4th is "60" Element 6th is "110" Element 8th is "180"
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *