EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Top Differences Tutorial C# List vs Array

C# List vs Array

Priya Pedamkar
Article byPriya Pedamkar

Updated June 12, 2023

C# List vs Array

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

C# List vs Array Infographics

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 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 –

  1. Array Lists vs 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
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Popular Course in this category
C# Course Bundle - 24 Courses in 1 | 5 Mock Tests
 93+ Hours of HD Videos
24 Courses
5 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.6
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more