Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the non-generic IEnumerable and the generic IEnumerable<T>?

Sorry for such a vague question, but I have been searching around for the best part of a day, I have read article after article (and many questions here) but just cannot find an easy to understand answer.

I (think I) know what IEnumerable is for, but I just can't understand what it means when it is defined with a generic type argument, for example:

IEnumerable<int> test = method();

This is just driving me mad! Please put me out of misery and explain what it means?

like image 639
idiot Avatar asked Apr 03 '11 03:04

idiot


1 Answers

An IEnumerable is basically a collection of objects. It has the method GetEnumerator() which allows you to iterate through all of the objects in the enumerable.

An IEnumerable<int> is basically a collection of integers. It has the method GetEnumerator() which allows you to iterate through all of the integers in the enumerable.

IEnumerable<int> test = method(); means that method() is getting a collection if integers from somewhere. It could be a List, an array or some other data type, but it is definitely a group of them and they are all integers, and you have the ability to iterate through them.

This post may be helpful as well: What's the difference between IEnumerable and Array, IList and List?

like image 175
smartcaveman Avatar answered Oct 25 '22 08:10

smartcaveman