Should i use icollection or ienumerable




















A base interface for collections in the System. This allows a generic collection to be passed to a method that expects an IEnumerable object. This means that all objects matching the original query will be loaded into memory from database.

If filtering is applied to the original query, it will be executed after the objects have been loaded into memory. In the example below, Take 2 extension method is applied only after the results have been loaded to memory.

Notice the SQL statement generated. In the above example, the SQL statement executed at database layer returns all records that match the WHERE criteria, even though we are looking for first two records.

Take 2 filter is applied only after the results have been loaded into the application's memory. As a base interface for collections, IEnumerable can work with nearly all collection types. Returns a collection of elements that contains the ancestors of every node in the source collection. Returns a filtered collection of elements that contains the ancestors of every node in the source collection. Only elements that have a matching XName are included in the collection.

Returns a collection of the descendant nodes of every document and element in the source collection. Returns a collection of elements that contains the descendant elements of every element and document in the source collection. Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Returns a collection of the child elements of every element and document in the source collection.

Returns a filtered collection of the child elements of every element and document in the source collection. Returns a collection of nodes that contains all nodes in the source collection, sorted in document order. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.

Contents Exit focus mode. Generic Assembly: System. Exposes the enumerator, which supports a simple iteration over a collection of a specified type. T The type of objects to enumerate. This type parameter is covariant.

That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics. Is this page helpful? Yes No. Any additional feedback? Skip Submit.

Constructs an immutable dictionary based on some transformation of a sequence. Enumerates and transforms a sequence, and produces an immutable dictionary of its contents. Enumerates a sequence and produces an immutable hash set of its contents. Enumerates a sequence and produces an immutable list of its contents.

Enumerates a sequence and produces an immutable sorted set of its contents. Returns distinct elements from a sequence according to a specified key selector function. Produces the set difference of two sequences according to a specified key selector function. Returns the first element in a sequence that satisfies a specified condition. Groups the elements of a sequence according to a specified key selector function. Produces the set intersection of two sequences according to a specified key selector function.

Returns the last element of a sequence that satisfies a specified condition. Returns the last element of a sequence, or a default value if the sequence contains no elements. Returns the maximum value in a generic sequence according to a specified key selector function. Returns the minimum value in a generic sequence according to a specified key selector function.

Sorts the elements of a sequence in ascending order according to a key. Sorts the elements of a sequence in ascending order by using a specified comparer. This is all reasonable, rational, logical and makes sense that System. ICollection interface inherits from System. IEnumerable interface, because theoretically every collection is also both enumerable and iterable and this is theoretically possible to go over all the items and elements in every collection.

ICollection interface represents a finite dynamic collection that are changeable, which means that exist items can be removed from the collection and new items can be added to the same collection. Because that instances of System. ICollection interface are finite collections then the word "finite" implies that every collection of this interface always has a finite number of items and elements in it. IEnumerable interface does not have these methods and properties that System.

ICollection interface has, because it does not make any sense that System. IEnumerable will have these methods and properties that System. ICollection interface has. The logic also says that every instance that is both enumerable and iterable is not necessarily a collection and not necessarily changeable.

When I say changeable, I mean that don't immediately think that you can add or remove something from something that is both enumerable and iterable. If I just created some finite sequence of prime numbers, for example, this finite sequence of prime numbers is indeed an instance of System. IEnumerable interface, because now I can go over all the prime numbers in this finite sequence in a single loop and do whatever I want to do with each of them, like printing each of them to the console window or screen, but this finite sequence of prime numbers is not an instance of System.

ICollection interface, because this is not making sense to add composite numbers to this finite sequence of prime numbers. Also you want in the next iteration to get the next closest larger prime number to the current prime number in the current iteration, if so you also don't want to remove exist prime numbers from this finite sequence of prime numbers.

Also you probably want to use, code and write "yield return" in the GetEnumerator method of the System. IEnumerable interface to produce the prime numbers and not allocating anything on the memory heap and then task the Garbage Collector GC to both deallocate and free this memory from the heap, because this is obviously both waste of operating system memory and decreases performance.

Dynamic memory allocation and deallocation on the heap should be done when invoking the methods and properties of System. ICollection interface, but not when invoking the methods and properties of System.

IEnumerable interface although System. IEnumerable interface has only 1 method and 0 properties. According to what others said in this Stack Overflow webpage, System. IList interface simply represents an orderable collection and this explains why the methods of System. IList interface work with indexes in contrast to these of System.

ICollection interface. In short System. ICollection interface does not imply that an instance of it is orderable, but System. IList interface does imply that. This also makes sense and explains why System. IList interface inherits System. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

If you only want to know when to use which type, scroll down and have a look at the table providing the scenarios and the relevant types. I strongly recommend reading of the entire article to get a deeper understanding. First of all, it is important to understand, that there are two different interfaces defined in the. NET base class library. The IEnumerable interface is located in the System. Collections namespace and contains only a single method definition.

The interface definition looks like this:. The GetEnumerator method must return an instance of an object of a class which implements the IEnumerator interface. It is important to know that the C language foreach keyword works with all types that implement the IEnumerable interface. I believe you have been using the foreach keyword many times and without worrying about the reason why and how it worked with that type.

Generic namespace :. Please take a look at the official msdn documentation if you would like to get some more information. As you can imagine, there are also two versions of ICollection which are System. ICollection and the generic version System.

ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface. I just want to let you know about the official description from the msdn documentation:.



0コメント

  • 1000 / 1000