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 the differences between a list collection and an array in C#?
List collection is a generic class that can store any data type to create a dynamic collection. Arrays, on the other hand, store a fixed-size sequential collection of elements of the same type. Understanding the differences between these two collection types is crucial for choosing the right approach for your application.
Syntax
Following is the syntax for declaring and initializing a List −
List<dataType> listName = new List<dataType>();
Following is the syntax for declaring and initializing an array −
dataType[] arrayName = new dataType[size];
dataType[] arrayName = {value1, value2, value3};
Using List Collection
Lists are dynamic collections that can grow and shrink at runtime. Elements are added using the Add() method −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<string> fruits = new List<string>();
// Adding elements
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");
Console.WriteLine("List elements:");
foreach(string fruit in fruits) {
Console.WriteLine(fruit);
}
Console.WriteLine("Count: " + fruits.Count);
// Remove an element
fruits.Remove("Banana");
Console.WriteLine("After removing Banana, Count: " + fruits.Count);
}
}
The output of the above code is −
List elements: Apple Banana Orange Count: 3 After removing Banana, Count: 2
Using Arrays
Arrays have a fixed size that must be specified at declaration time. Once created, the size cannot be changed −
using System;
class Program {
public static void Main() {
// Method 1: Declare and initialize separately
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
// Method 2: Initialize with values
string[] colors = {"Red", "Green", "Blue"};
Console.WriteLine("Array elements:");
for(int i = 0; i < colors.Length; i++) {
Console.WriteLine(colors[i]);
}
Console.WriteLine("Numbers array length: " + numbers.Length);
Console.WriteLine("First number: " + numbers[0]);
}
}
The output of the above code is −
Array elements: Red Green Blue Numbers array length: 5 First number: 10
Key Differences
| Feature | List | Array |
|---|---|---|
| Size | Dynamic (can grow/shrink) | Fixed size |
| Memory | Allocated on heap | Can be on stack or heap |
| Performance | Slightly slower due to dynamic resizing | Faster access and less memory overhead |
| Methods | Rich set of methods (Add, Remove, Contains, etc.) | Limited built-in methods |
| Type Safety | Generic - type safe | Type safe |
Performance Comparison Example
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
// List operations
List<int> numbersList = new List<int>();
numbersList.Add(1);
numbersList.Add(2);
numbersList.Add(3);
numbersList.Insert(1, 10); // Insert at index 1
Console.WriteLine("List after insertion:");
foreach(int num in numbersList) {
Console.Write(num + " ");
}
Console.WriteLine();
// Array operations
int[] numbersArray = new int[4] {1, 2, 3, 4};
Console.WriteLine("Array elements:");
for(int i = 0; i < numbersArray.Length; i++) {
Console.Write(numbersArray[i] + " ");
}
Console.WriteLine();
// Array access is direct
Console.WriteLine("Third element in array: " + numbersArray[2]);
Console.WriteLine("Third element in list: " + numbersList[2]);
}
}
The output of the above code is −
List after insertion: 1 10 2 3 Array elements: 1 2 3 4 Third element in array: 3 Third element in list: 2
When to Use Which
Use Lists when you need a dynamic collection that can grow or shrink during runtime, when you frequently add or remove elements, or when you need built-in methods for manipulation.
Use Arrays when you know the exact size beforehand, when performance is critical, when working with mathematical operations, or when memory usage needs to be minimized.
Conclusion
Lists provide dynamic sizing and rich functionality at the cost of slightly more memory and processing overhead, while arrays offer better performance and memory efficiency with a fixed size constraint. Choose based on your specific requirements for flexibility versus performance.
