Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are dynamic arrays in C#?
Dynamic arrays are growable arrays that can resize themselves during runtime, providing a significant advantage over static arrays which have a fixed size. In C#, you can create dynamic arrays using collections like ArrayList and the more modern List<T>.
These dynamic collections allow automatic memory allocation, resizing, adding, searching, and sorting items efficiently. The ArrayList stores objects of any type, while List<T> is type-safe and performs better.
Syntax
Following is the syntax for creating an ArrayList −
ArrayList arrayList = new ArrayList(); arrayList.Add(item);
Following is the syntax for creating a generic List −
List<T> list = new List<T>(); list.Add(item);
Using ArrayList for Dynamic Arrays
ArrayList represents an ordered collection of objects that can be indexed individually. It automatically grows as you add elements −
Example
using System;
using System.Collections;
class Program {
static void Main(string[] args) {
ArrayList al = new ArrayList();
al.Add(577);
al.Add(286);
al.Add(145);
Console.WriteLine("Count: {0}", al.Count);
Console.Write("List: ");
foreach (int i in al) {
Console.Write(i + " ");
}
Console.WriteLine();
// Demonstrating dynamic growth
al.Add("Hello"); // Can add different types
al.Add(3.14);
Console.WriteLine("After adding mixed types - Count: {0}", al.Count);
Console.Write("Mixed List: ");
foreach (object item in al) {
Console.Write(item + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
Count: 3 List: 577 286 145 After adding mixed types - Count: 5 Mixed List: 577 286 145 Hello 3.14
Using List<T> for Type-Safe Dynamic Arrays
The generic List<T> class is preferred over ArrayList as it provides type safety and better performance −
Example
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
List<int> numbers = new List<int>();
// Adding elements
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
Console.WriteLine("Count: " + numbers.Count);
Console.WriteLine("Capacity: " + numbers.Capacity);
Console.Write("Numbers: ");
foreach (int num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine();
// Inserting at specific position
numbers.Insert(1, 15);
Console.Write("After Insert: ");
foreach (int num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine();
// Removing elements
numbers.Remove(20);
Console.Write("After Remove: ");
foreach (int num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
Count: 3 Capacity: 4 Numbers: 10 20 30 After Insert: 10 15 20 30 After Remove: 10 15 30
ArrayList vs List<T> Comparison
| Feature | ArrayList | List<T> |
|---|---|---|
| Type Safety | No - stores objects | Yes - strongly typed |
| Performance | Slower due to boxing/unboxing | Faster - no boxing |
| Memory Usage | Higher - stores references | Lower - direct storage |
| Compile-time Checking | Limited | Full type checking |
Common Operations
Example
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
List<string> fruits = new List<string>();
// Adding elements
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");
Console.WriteLine("Original list:");
fruits.ForEach(fruit => Console.Write(fruit + " "));
Console.WriteLine();
// Sorting
fruits.Sort();
Console.WriteLine("Sorted list:");
fruits.ForEach(fruit => Console.Write(fruit + " "));
Console.WriteLine();
// Finding elements
bool hasApple = fruits.Contains("Apple");
Console.WriteLine("Contains Apple: " + hasApple);
int index = fruits.IndexOf("Banana");
Console.WriteLine("Index of Banana: " + index);
}
}
The output of the above code is −
Original list: Apple Banana Orange Sorted list: Apple Banana Orange Contains Apple: True Index of Banana: 1
Conclusion
Dynamic arrays in C# provide flexible, growable collections that automatically manage memory allocation. While ArrayList offers flexibility for mixed data types, List<T> is preferred for its type safety, better performance, and compile-time error checking, making it the modern choice for dynamic arrays in C#.
