Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between string array and list of string in c#

Tags:

arrays

string

c#

People also ask

What is the difference between String [] and list string?

String[] is an array of Strings while ArrayList is a generic class which takes different types of objects (here it takes Strings). Therefore you can only perform normal array operations with String[].

What is the difference between string and string array in C?

What is the Difference Between Array and String. The main difference between Array and String is that an Array is a data structure that stores a set of elements of the same data type while a String is a set of characters. Programming languages such as C supports arrays and strings.

What is the difference between array and list in C?

What is the difference between a list and an array in C#? An array stores a fixed-size sequential collection of elements of the same type, whereas list is a generic collection.

What is the difference between array and list?

List is used to collect items that usually consist of elements of multiple data types. An array is also a vital component that collects several items of the same data type. List cannot manage arithmetic operations. Array can manage arithmetic operations.


Arrays are a lower level abstraction than collections such as lists. The CLR knows about arrays directly, so there's slightly less work involved in iterating, accessing etc.

However, this should almost never dictate which you actually use. The performance difference will be negligible in most real-world applications. I rarely find it appropriate to use arrays rather than the various generic collection classes, and indeed some consider arrays somewhat harmful. One significant downside is that there's no such thing as an immutable array (other than an empty one)... whereas you can expose read-only collections through an API relatively easily.


The article is from 2004, that means it's about .net 1.1 and there was no generics. Array vs collection performance actually was a problem back then because collection types caused a lot of exta boxing-unboxing operations. But since .net 2.0, where generics was introduced, difference in performance almost gone.


An array is not resizable. This means that when it is created one block of memory is allocated, large enough to hold as many elements as you specify.

A List on the other hand is implicitly resizable. Each time you Add an item, the framework may need to allocate more memory to hold the item you just added. This is an expensive operation, so we end up saying "List is slower than array".

Of course this is a very simplified explanation, but hopefully enough to paint the picture.


An array is the simplest form of collection, so it's faster than other collections. A List (and many other collections) actually uses an array internally to hold its items.

An array is of course also limited by its simplicity. Most notably you can't change the size of an array. If you want a dynamic collection you would use a List.