8 Replies - 22838 Views - Last Post: 25 October 2019 - 07:02 PM Rate Topic: -----

#1 BiffBaffBoff   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 77
  • Joined: 10-February 11

Question: Difference between Array, List, IEnumerable?

Posted 28 August 2011 - 11:10 AM

Hi guys,

Basically I'd like to know the difference between these 3 and when is the best situation to use all 3. Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Question: Difference between Array, List, IEnumerable?

#2 Joyal1234   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 21-August 11

Re: Question: Difference between Array, List, IEnumerable?

Posted 28 August 2011 - 11:24 AM

I don't know much about IEnumarable but I can give you the difference between Array and List.

List can be used over Array for run time memory allocation. List provides many in built functions that can be used for allocation, manipulation and access of the members in the list.
Was This Post Helpful? 1
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Question: Difference between Array, List, IEnumerable?

Posted 28 August 2011 - 11:42 AM

Hi,

Arrays

An array is the most basic container. It allows objects to be stored and accessed individually using an index. It is a good container to use when you know how many items you want to store. The array must have its size set at compile time, and it (see edit below, and Momerath's post) doesn't automatically resize if you try and add more elements at runtime (it will just throw an exception). This can be good, as resizing involves moving elements around, and it can be expensive; particularly with large numbers of elements.

You have to resize the array explicitly by calling Array.Resize().

List<T>

A List<T> uses an array internally to store its elements, so it is just a fancy wrapper class for the standard array. The obvious reason to use a List<T> instead of a raw array is when you don't know at compile time how many elements are going to be held in the collection.

However, the List<T> will automatically resize itself for you at runtime, as you add more elements, and remove elements. This is a great convenience if you don't know how many elements will be added at runtime.

List<T> also still supports accessing of elements via an integer index, just like arrays too.

It provides some other extra functionality compared to a basic array, and has some extra methods (like Remove()) that make the List<T> very easy to use, and more practical in the real world. Besides this extra functionality, it still has good performance as it is optimised, and still just using an array behind the scenes.


When to use Array, and when to use List<T>


In extremely performance critical situations (particuarly where the number of elements you're holding is high, and perhaps when performing lots of random access operations), you may use an array as an optimisation, as you would avoid the extra layer of indirection (and therefore a lesser degree of control) the List<T> class introduces, but that would be relatively rare I think. In that scenario, you really would have to do some performance analysis, and made a decision based on your exact scenario, as the performance of each varies with different scenarios.


I would say that a List<T> is generally a safe, practical bet since it handles some very common, mundane tasks for you (like resizing), making your life much easier. If you find yourself resizing anything like often, then a List<T> is really a 'no brainer'. Just keep the possible differences (usually they are slight, and won't concern you anyway) in performance in the back of your mind (both for and against the List<T>), just in case you encounter a situation where it becomes important/an issue :)

Having said that, if/when you encounter a scenario where you really simply don't need the 'bells and whistles' offered by List<T>, and an array suits your needs just fine (no/very very little resizing needed now or in future etc); great! Don't use a list just for the sake of it, but for many real world tasks, it will make your life easier :)


IEnumerable and IEnumerable<T>

IEnumerable is an interface that represents the ability to iterate through the collection using an iterator. For example, anything that implements IEnumerable can be iterated through using a for-each loop. Both the Array and List<T> classes implement the interface, and so can be used in a for-each loop.

It isn't a collection in itself, it just represents a behaviour that actual collections may (and usually do) have.

The generic equivalent of IEnumerable (IEnumerable<T>) also serves as the source of many LINQ extension methods also, as LINQ extensions usually require the ability to enumerate over collections, and anything that implements IEnumerable<T> will have that behaviour.

EDIT: Ignore the 'size must be known at compile time' comment, as Momerath very rightly pointed out below. I got myself mixed up, and made a misleading statement.

An array's size must be specified at compile time in that a size must be given in the [] (or implicitly inferred), at compile time. However, that size value can be either a value hardcoded at compile time, or a variable whose value can be determined at runtime (as in Momerath's example). The actual size value doesn't have to be known compile time, as my statement seemed to suggest :)

This post has been edited by [email protected]: 28 August 2011 - 04:39 PM

Was This Post Helpful? 3
  • +
  • -

#4 BiffBaffBoff   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 77
  • Joined: 10-February 11

Re: Question: Difference between Array, List, IEnumerable?

Posted 28 August 2011 - 11:55 AM

Thanks for the fast reply guys, its much appreciated. I understand now. Basically use a List when you know it's size is going to change during run-time. So an example would be some kind of storage system whereby a user can add/remove items stored?
Was This Post Helpful? 0
  • +
  • -

#5 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Question: Difference between Array, List, IEnumerable?

Posted 28 August 2011 - 12:25 PM

View Post[email protected], on 28 August 2011 - 11:42 AM, said:

The array must have its size set at compile time, and it doesn't automatically resize if you try and add more elements at runtime (it will just throw an exception).


An array does not have to have it's size set at compile time. For example:
int[] myArray;
int size;
Boolean badInput = true;
do {
    Console.WriteLine("Enter size of the array");
    String input = Console.ReadLine();
    if (Int32.TryParse(input, out size) {
        badInput = false;
    }
} while (badInput);
myArray = new int[size];


An array can also be resized during runtime
Array.Resize(ref myArray, size + size);


Resizing an array is considered an 'expensive' operation, as new memory has to be allocated and the original array copied over. But most generic collections are actually backed by an array. The advantages of collections is they handle the insert, removal and resizing of the backing array for you, freeing you up to worry about what you want to accomplish.

Edit: You edited your post while I was writing mine and you included some of what I had to say :)

This post has been edited by Momerath: 28 August 2011 - 12:27 PM

Was This Post Helpful? 1
  • +
  • -

#6 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Question: Difference between Array, List, IEnumerable?

Posted 28 August 2011 - 12:58 PM

View PostBiffBaffBoff, on 28 August 2011 - 06:55 PM, said:

So an example would be some kind of storage system whereby a user can add/remove items stored?


Yes, that's a prime candidate for a List<T>. You would be able to just add the elements as the user entered them (using Add()), and remove elements (using Remove())) with the minimum of fuss. Both you and your users are happy.


If you used an array for that purpose, you would likely have to make the user enter the number of entries they are going to make before they started (which isn't very friendly :)).

Or, you would end up creating an array with a small initial size, and resizing the array manually which increases the complexity of your code (essentially needlessly recreating the functionality of List<T> manually).

Or, as I have seen some people do, you could create an array that is so large, that no user will ever realistically exceed its bounds; which isn't a particularly elegant/good way to do things).

Removing of items would be a real pain too if you used arrays...


A much used example of when using an array is a good choice is in implementing the tic tac toe game. An array is perfect to use to represent the board, as the size of the board will not change in any way at run time, and the extra methods that List<T> exposes simply aren't needed in that simple scenario, so why use it?

Unfortunately, in the real world, the scenario is rarely that simple, which is why List<T> was introduced, to make our lives easier :).

This post has been edited by [email protected]: 28 August 2011 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Question: Difference between Array, List, IEnumerable?

Posted 28 August 2011 - 03:16 PM

The differences between them is to do with there different asymptotic complexities and semantics. (Resizing and access and index).
Remember that coping memory blocks is slow.

IEnumerable is a Read Only view of the underlying collection. The pattern it uses is the basis of LINQ and has index access of O(N)

Array and List<T> have different re-sizing complexities.
Increase the size by one and array by one 1 the capacity is re-sized by one. Whereas with a list it doubles the capacity, until that capacity has been reached, where it double in capacity again.. Reducing the number time the collection has to be re-size.
Was This Post Helpful? 1
  • +
  • -

#8 shadmansk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-October 19

Re: Question: Difference between Array, List, IEnumerable?

Posted 25 October 2019 - 11:51 AM

To understand the difference you need to understand its use. Actually, they represent an Iterator design pattern which is one of the design patterns used by object-oriented software developers.

The Iterator pattern provides a way of accessing elements of a collection sequentially, without knowing how the collection is structured.

In the traditional design pattern approach, the iterator pattern has an Aggregate interface for creating an Iterator object and the Iterator interface for traversing an Aggregate object.

Now, IEnumerable acts as an Aggregate interface that guarantees to return an iterator. Iterators (also known as Enumerators) are responsible for producing the next element in a sequence defined by certain criteria. IEnumerator is an interface that defines this functionality. You can learn more about this pattern in my blog post Iterator Pattern C#

This post has been edited by modi123_1: 25 October 2019 - 03:14 PM
Reason for edit:: spammy link removed

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7141
  • View blog
  • Posts: 24,251
  • Joined: 05-May 12

Re: Question: Difference between Array, List, IEnumerable?

Posted 25 October 2019 - 07:02 PM

shadmansk: In the future please do not revive old threads, especially old threads where the original person asking the question has not logged in in over 7 years, and the thread is over 8 years old.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1