In This Tutorial
- 1 What is the ArrayList?
- 2 Syntax to initialize an ArrayList
- 3 An ArrayList example with the Add method
- 4 ArrayList with mixed items example
- 5 ArrayList example with foreach
- 6 C# ArrayList sort method example
- 7 Related
What is the ArrayList?
The ArrayList is a class of System.Collection namespace in C# which is just like the arrays. However, unlike simple arrays, the ArrayList in C# is dynamic.
That means you can add or remove elements from the ArrayList by using the index number dynamically. While arrays are fixed in size.
The ArrayList dynamically resizes itself as items are added or removed. Also, the ArrayList can contain mixed items like numbers and strings etc. in a single ArrayList, unlike a simple array.
See an ArrayList example
The C# ArrayList has its methods and properties that can be used to perform desired tasks like count, Capacity, Add, Clear, sort etc. We will show you examples of using the ArrayList class with its methods and properties in this tutorial, let us look at its syntax first.
Syntax to initialize an ArrayList
Following is the general syntax to create/initialize a C# ArrayList object:
ArrayList ArrLst = new ArrayList();
Where
- The ArrayList is followed by the name of the object, ArrLst in that case.
- You have to use the new keyword which is followed by the ArrayList and parenthesis.
As we learned, what ArrayList is and its syntax, let us look at a few examples of creating and using properties and methods of the ArrayList of C#.
An ArrayList example with the Add method
In this example, we will create an ArrayList object and its items. After that, we will use Add method of the ArrayList class to create items. Also, we will use ArrayList Count property to get the total count of array list in the for loop.
In that example, we will just assign numeric values and then use for loop to display ArrayList items.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Collections;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
ArrayList ArrLst = new ArrayList();
ArrLst.Add(10);
ArrLst.Add(20);
ArrLst.Add(30);
ArrLst.Add(40);
for (int i = 0; i < ArrLst.Count; i++)
{
Console.WriteLine(ArrLst[i]);
}
Console.ReadLine();
}
}
}
|
You can see, the ArrayList items are displayed by using a for loop.
ArrayList with mixed items example
In this example, we will create a C# ArrayList with mixed items i.e. numbers and strings. After creating a list, we will display ArrayList items by using a for loop. See example by clicking the link below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
using System;
using System.Collections;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
ArrayList ArrLst = new ArrayList();
ArrLst.Add(1);
ArrLst.Add(“This”);
ArrLst.Add(2);
ArrLst.Add(“is”);
ArrLst.Add(3);
ArrLst.Add(“ArrayList”);
ArrLst.Add(4);
ArrLst.Add(“Tutorial”);
for (int i = 0; i < ArrLst.Count; i++)
{
Console.WriteLine(ArrLst[i]);
}
Console.ReadLine();
}
}
}
|
You can see, we used numbers and strings in a single ArrayList C#. This is unlike simple arrays and list in C#, that uses strict type.
ArrayList example with foreach
In above examples, we used for loop to iterate through ArrayList. In this example, we are using the foreach loop to iterate through ArrayList items. See example by clicking the link below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Collections;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
ArrayList tstlist = new ArrayList();
tstlist.Add(10);
tstlist.Add(20);
tstlist.Add(30);
tstlist.Add(40);
foreach (int i in tstlist)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
|
To learn more about c# foreach loop go to its chapter.
C# ArrayList sort method example
Following is an example of using the ArrayList sort method. For that, we have created an array with the unordered list.
In the example, we will first display ArrayList items in the same order as we created and then we used the sort method and displayed the ArrayList again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
using System;
using System.Collections;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
ArrayList tstlist = new ArrayList();
tstlist.Add(70);
tstlist.Add(50);
tstlist.Add(20);
tstlist.Add(40);
Console.WriteLine(“ArrayList before sort”);
foreach (int i in tstlist)
{
Console.WriteLine(i);
}
Console.WriteLine(“ArrayList after sort”);
tstlist.Sort();
foreach (int i in tstlist)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
|
You can see, the sort method sorted the ArrayList from lower to higher numbers.
Further Reading: C# list | C# Array
Leave A Comment?