IEnumerable <T> vs IReadOnlyList <T>

What is the difference between choosing IEnumerable<T> vs IReadOnlyList<T> as the type of the return parameter or the type of the input parameter?

IEnumerable<T> provides .Count and .ElementAt that is displayed by IReadOnlyList<T>

+6
source share
1 answer

IEnumerable<T> is a direct pointer to some data. You can go from beginning to end of a collection by looking at one item at a time.

IReadOnlyList<T> is a readable random access collection.

IEnumerable<T> more general because it can represent elements created on the fly, data coming in over the network, rows from the database, etc. IReadOnlyList<T> , on the other hand, basically represents only collections in memory.

If you only need to look at each element once, in order, then IEnumerable<T> is the best choice - it is more general.

I would recommend actually looking at the standard C ++ template library - their discussion of the various types of iterators really reflects your question well.

+12
source

Source: https://habr.com/ru/post/980747/


All Articles