Updated June 12, 2023
Differences Between C# List and Array
C# List <T> class represent a strongly typed list of objects that can be accessed by the index and it supports storing values of a specific type without casting to or from an object.
List<T>, where parameter T is the type of elements in the List.
Features
Certain features of the List are given below:
Add integer values in the List collection:
List<int> intList = new List<int>();
intList.Add (2);
intList.Add (3);
intList.Add (4);
Add String values in the List collection:
List<String> colors = new List<String>();
colors.add ("red");
colors.add ("white");
colors.add ("green");
Retrieve items from a List collection by using for loop:
foreach(string color in colors) {
MessageBox.Show(color)
}
Declare an array in C#:
datatype[] typeName;
// DataType is used to specify the type of elements in the array.
Initializing an array :
double[] balance = new double[50];
Assigning values to an array:
double[] balance = {23.0, 34.56, 44.32};
Accessing array elements:
Foreach (int value in balance) {
Console.WriteLine ("element is : "+ value);
}
Create and initialize an array at the same time:
Int[] steps = new int[6] {1, 2, 3, 4, 5, 6};
Copy an array variable into another target array variable:
Int[] count = steps;
Both target and source point to the same memory location.
Head To Head Comparison Between C# List and Array (Infographics)
An array stores a fixed-size sequential collection of elements of the same type. It is used to store a data collection, but the array can be considered a collection of variables of the same type stored at contiguous memory locations. All arrays consist of contiguous memory locations, with the lowest address corresponds to the first element and the highest address to the last element.
Below are the top 8 differences between C# List vs Array
Key Differences between C# List and Array
Both are popular choices in the market; let us discuss some of the major differences:
- The list is built on the top of Array, whereas Array is a lower-level data structure.
- A list is shipped in the form of APIs in C# with a parent as Collection class, whereas Array is the lower level data structure with its own specific properties.
- A list is not index-based, based on the concept of nodes, whereas Arrays are index-based data structures with the lowest address is provided to the first element, and the highest address is provided to the last element in the array.
- List are dynamic in nature, i.e. their size automatically increases with more element insertion, whereas Arrays are the fixed-size structure, once initialized, cannot be reset.
- The list is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario.
- List occupies much more memory as every node defined the List has its own memory set whereas Arrays are memory-efficient data structure.
- A list is derived from Collection, which contains a more generic data type, whereas Array is fixed and store a more strong data type.
- The list contains nodes that have memory locations that need not be contiguous in nature, whereas Array contains the elements with their memory location, which are contiguous in nature.
- The non-contiguous property of List makes them take more time in accessing the elements, whereas the contiguous property of Array makes them highly efficient for accessing the elements.
- List leverage generics is basically a type-safe version of ArrayList and generates a compile-time error, whereas Arrays, with its type-safe, highly efficient in terms of speed and performance, supports multiple dimensions.
C# List vs Array Comparison Table
Below is the topmost comparison
Basis Of Comparison | List | Array |
Creation | Built on the top of Array | The original data structure, based on the index concept |
Memory | Occupy more memory than Array | Memory-efficient |
Length | Length varies | Fixed size length |
Usage | Frequent insertion and deletion | Frequent element access |
Resize | Resize List is dynamic in nature | Resizing arrays is expensive |
Structure | Non-contiguous memory | Contiguous memory location |
Indexing | Non- index based structure | Index based with the lowest address as first, and highest address as last |
Access | Access element is time-consuming although based on the position of an element. | Access element is a constant time operation irrespective of element location. |
Conclusion
Both C# List vs Array are distinct types, having different capabilities and store their data in separate ways. The storing capabilities and design of both data structures make them unique in their own ways. An Array is fixed in size, and once it is allocated, one cannot add or remove items from it; also, all elements must be of the same type. Thus, it is a type-safe and most efficient linear data structure in terms of speed and performance. Also, Array supports multiple dimensions. The list provides more generic capabilities and derived from Collection APIs. Unlike Array, they are dynamic in nature, can resize automatically with frequent insertion and deletion of elements. It is essentially a type-safe version of an ArrayList data structure. Type safety feature means there is no boxing or unboxing, which would enhance performance, and if anyone attempts to add an element of the wrong type, it will generate a compile-time error.
C# List vs Array performance is a linear data structure that is well suited for different scenarios. If frequent insertion and deletion occur, and at the same time, memory is not a constraint, then List is an ideal choice, whereas in scenarios like frequent access of elements of required with a memory constraint, then Array is a better option. It all depends upon the use case and requirement. An array is always a list in nature, but a list is not an array. The array allows both kinds of access, direct and sequential, while List only allows sequential access. And this is because of the way these data structures are stored in memory. Since List is derived from Collection, it can use different implementations; one of these implementations is ArrayList, a class that implements the List’s behaviour using arrays as a data structure. An Array is very much tied to the hardware notion of continuous, contiguous memory, with each element identical in size. Both performance ideas line up quite well, based on scenarios. It all boils down to the requirement at the end of the day, although the memory part can be safely side-line in today’s world since high memory has become a norm.
Recommended Article
This has been a guide to the top differences between C# List vs Array. Here we also discuss the key differences with infographics and comparison table. You may also have a look at the following articles –