Difference between list and array C#

Difference between list and array C#

Differences Between C# List and Array

C# List 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, where parameter T is the type of elements in the List.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Features

Certain features of the List are given below:

Add integer values in the List collection:

List intList = new List();
intL)ist.Add (2);
intList.Add (3);
intList.Add (4);

Add String values in the List collection:

List colors = new List();
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

Popular Course in this category
Difference between list and array C#
C# Training Program (6 Courses, 17 Projects)6 Online Courses | 17 Hands-on Project | 89+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.6 (11,723 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)Python Training Program (39 Courses, 13+ Projects)HTML Training (12 Courses, 19+ Projects, 4 Quizzes)

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

Difference between list and array C#

Key Differences between C# List and Array

Both are popular choices in the market; let us discuss some of the major differences:

  1. The list is built on the top of Array, whereas Array is a lower-level data structure.
  2. 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.
  3. 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.
  4. 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.
  5. The list is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario.
  6. List occupies much more memory as every node defined the List has its own memory set whereas Arrays are memory-efficient data structure.
  7. A list is derived from Collection, which contains a more generic data type, whereas Array is fixed and store a more strong data type.
  8. 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.
  9. 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.
  10. 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 ComparisonListArray
CreationBuilt on the top of ArrayThe original data structure, based on the index concept
MemoryOccupy more memory than ArrayMemory-efficient
LengthLength variesFixed size length
UsageFrequent insertion and deletionFrequent element access
ResizeResize List is dynamic in natureResizing arrays is expensive
StructureNon-contiguous memoryContiguous memory location
IndexingNon- index based structureIndex based with the lowest address as first, and highest address as last
AccessAccess 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 Lists 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 todays 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

  1. Array Listsvs Java List Valuable Differences
  2. Amazing Guide on C vs Java
  3. Java Vector and ArrayList
  4. C# vs Js Awesome Differences
  5. Require vs Import: Want to know the Benefits
  6. C vs C#: What are the Best Differences
  7. C# vs JavaScript: What are the Features

C# Training Program (6 Courses, 17 Projects)

6 Online Courses

17 Hands-on Project

89+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share