Articles → CSHARP → Arraylist In C#
Arraylist In C#
What Is An Arraylist?
Syntax
ArrayList obj = new ArrayList();
Namespace
Example
using System;
using System.Collections;
namespace Test1 {
class Program {
static void Main(string[] args) {
ArrayList obj = new ArrayList();
obj.Add("test1");
obj.Add(1);
obj.Add(2.03);
Console.WriteLine("Looping using foreach loop");
foreach(object o in obj) {
Console.WriteLine(o);
}
Console.WriteLine("Looping using for loop");
for (int i = 0; i < obj.Count; i++) {
Console.WriteLine(obj[i]);
}
Console.ReadLine();
}
}
}
- While declaring an arraylist, we have not defined the size.
- We can add items as objects which means that we can add data of different data types in the same arraylist.
- We can loop through an arraylist using for loop and foreach loop.
Output
Click to Enlarge