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

Sorry for such a vague question, but I searched for most of the day, I read the article after the article (and many questions here), but just can't find an easily understood answer.

I (I think) know what IEnumerable is for, but I just can't figure out what it means when it is defined using the generic type argument, for example:

IEnumerable<int> test = method(); 

It just drives me crazy! Please remove me from suffering and explain what it means?

+7
source share
5 answers

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

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

IEnumerable<int> test = method(); means method() gets the collection if integers are somewhere. It may be a list, an array, or some other data type, but it is definitely a group of them, and they are all integer, and you have the option of iterating through them.

This post may be useful: What is the difference between IEnumerable and Array, IList and List?

+13
source

I just think of IEnumerable<int> same way I think of List<int> , which looks a little more natural, I suppose. With the caveat that IEnumerable<int> doesn't do as much as List<int> , and that essentially this is just an int thing that can be listed

+2
source

An IEnumerable has a GetEnumerator method that returns an IEnumerator , the Current method returns an Object . IEnumerable<T> has a GetEnumerator method that returns IEnumerator<T> , the Current method returns T If you know in advance the expected type of object to be returned by the enumerator, it is usually best to use a general form.

Another difference is that IEnumerator<T> inherits IDisposable , which allows you to use code that is done with an enumerator to invoke Dispose on it, without worrying about whether it is supported. For comparison, when using a non-generic IEnumerator you need to check if this is IDisposable and call Dispose on it, if so. Note that using the non-core IEnumerable / IEnumerator form does not free one of the requirements for calling Dispose . For example, casting a vb-style collection to IEnumerable and then calling GetEnumerator 100,000 times without calling Dispose will be extremely slow (many seconds, even on i7) if garbage collection does not happen. By IEnumerator after each call, the speed will be more than a hundred times.

+2
source

The word you are looking for is "generics", and the example you give is IEnumerable, which is used as a generic for elements of type int. This means that the IEnumerable collection you are using is strongly typed to hold only int objects, unlike any other type.

Google "C # generics IEnumerable" and you will find all the necessary information on this.

0
source

IEnumerable means that it can be used in a foreach loop.

test can be used as

 foreach(int item : test) { console.writeline(item); } 
0
source

All Articles