What is the difference between returning IList vs List or IEnumerable vs List <Class>. I want to know what is best to return

What is the difference between returning IList vs List or IEnumerable vs List.

I want to know it's better to come back.

When we need to use one, how will it affect performance?

+9
c # asp.net-mvc
source share
2 answers

There is no type that is always better to return. This is a decision that you must make based on your design / performance / etc goals.

IEnumerable<T> nice to use when you want to represent a sequence of elements that you can iterate over, but you don't want to allow modifications (Add, Delete, etc.).

IList<T> gives you everything you could use IEnumerable<T> , as well as operations that give you more control over the collection: add, delete, count, access the index, etc.

List<T> is a concrete implementation of IList<T> . I would say that it is almost always better to expose the IList<T> interface from your methods, rather than an implementation of List<T> . And it's not just lists - it's a basic design principle that prefers interfaces over specific implementations.

Well, now for the non-generic versions of IEnumerable, IList, List : They actually came from earlier versions of the .NET framework, and life is much better using common equivalents.

And a few words about performance: IEnumerable<T> (with IEnumerator<T> ) is actually an iterator that allows you to defer some calculations to the end. This means that there is no need to immediately allocate memory for data storage (of course, this is not the case when you have, say, an array behind an iterator). You can calculate data as needed. But this means that these calculations can be done over and over again (say, with each foreach ). On the other hand, with a list, you have fixed data in memory, with cheap indexes and counters. As you can see, the whole point is a compromise.

+11
source share

The use of specific classes in the parameters and results of methods makes a strong dependency, but the use of interfaces does not. What does it mean?

If in the future you change the implementation of your class and use SynchroinizedCollection , LinkedList or something else instead of List , then you must change your method signature, exactly this type of return value.

After that, you need to not only rebuild the assemblies that used this class, but you may have to rewrite them.

However, if you use one of the IEnumerable , IReadonlyCollection , IReadonlyCollection , IList interfaces, you do not have to rewrite and recompile client assemblies. Thus, interfaces have always preferred classes in parameters and results. (But remember, we are talking about dependencies between different assemblies. With the same assembly, this rule is not so important.)

The question is which interface to use? It depends on the requirements of the client classes (use cases). FE if you process the elements one by one, use IEnumerable<T> , and if you need the number of elements, use IReadonlyCollection<T> . Both of these interfaces are co-dispersion, convenient for type casting.

If you need the ability to write ( Add , Remove , Clear ) or the ability to read only the combined variance ( Contains ), use ICollection<T> . Finally, if you need random index access, use IList<T> .

As far as performance is concerned, calling an interface method is a bit slower, but that is a slight difference. You do not have to worry about that.

+4
source share

All Articles