Difference between array and arraylist in c#?
Loading
Difference between array and arraylist in c#?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Brahma Prakash ShuklaPosted Nov 1, 2022, 8:26 AM
An array can store primitive data as well as objects, but it cannot store generics. ArrayList is able to store generics as well as objects, but it cannot store data of primitive types
Muhammad Imran AnsariPosted Oct 12, 2022, 9:50 AM
The array is a specified-length data structure whereas ArrayList is a variable-length Collection class.
Array is strongly typed. This means that an array can store only specific type of items\elements whereas ArrayList can store any type of items\elements.
Examples:
Vishal YelvePosted Oct 11, 2022, 9:41 AM
Rajanikant HawaldarPosted Oct 11, 2022, 9:00 AM
1) Array belongs to the System namespace while ArrayList belongs to the System.Collections namespace.
2) An Array is strongly-typed so we can store only a similar type of data. While ArrayList is a non-generic collection type so we can store different types of data in ArrayList.
3) An array is a fixed size so it can store only a fixed number of elements. On the other hand, ArrayList is dynamic in terms of capacity, ArrayList will increase to double its current size if the number of elements exceeds.
4) We cannot insert any item in the middle of the array but we can insert an item in the middle of ArrayList.
5) We cannot delete any item from the middle of the array but we can remove an item from the middle of ArrayList.
6) Array provides better performance than ArrayList because it does not cause boxing and unboxing. But ArrayList involves boxing and unboxing.
7) The Array cannot accept null values while An ArrayList can accept null.
8) The array uses static helper class Array which provides support for handling the array but ArrayList implements an IList interface which provides different methods that are used for easy implementation.
https://www.c-sharpcorner.com/article/difference-between-array-and-arraylist-in-c-sharp/
https://www.c-sharpcorner.com/blogs/difference-between-array-and-arraylist-in-c-sharp
Toufik El AzzaouiPosted Oct 11, 2022, 8:52 AM
ArrayList is a non-generic collection class and is in the System.Collection namespace.
In the example above, we create an ArrayList object and add different data types to it. But in reality, ArrayList is a collection of type Object, and when we add an item to ArrayList, it first converts it to type Object, and then adds it to the collection.
Array is a sequential collection of elements with the same data type that are accessible via an index. the elements get stored in a contiguous memory location.
Differences between Array and ArrayList :
- Memory:
For Array the size is fixed.
For ArrayList the size is variable.
- Namespace:
For Array : System.Array
For ArrayList: System.Collection
- Data Type:
In Arrays, we can store only one datatype either int, string, char etc.
In ArrayList we can store different datatype variables.
- Null :
Array does not accept null.
ArrayList accepts null.
Please refer to this link for more information:
https://www.c-sharpcorner.com/article/difference-between-array-and-arraylist-in-c-sharp/