Ask Your Question
4

Which is the preferred option to return - IList vs List or IEnumerable vs List<Class> - and what distinguishes them from each other?

asked 2022-11-13 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-02-20 00:00:00 +0000

scrum gravatar image

The preferred option to return would depend on the context of your application.

If you want to allow other developers to modify the items in the list, then you should return a List. In contrast, if you want to prevent modification of the list, you should return an IList or IEnumerable.

An IList is an interface that defines a read-write collection of elements, and it is preferred when you want to keep the collection ordered, or you need to insert or remove elements. On the other hand, IEnumerable is an interface that provides a read-only access to a collection of elements that can be enumerated.

List is a concrete implementation of the IList interface while List<class> is a generic List that takes a type parameter. The main difference between the two is that List<class> is strongly typed, meaning that it can only hold objects of the specified class type, while List can hold any type of object.

Overall, the choice between these options will depend on the specific requirements of your application, and you should choose the one that best fits your needs.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-13 11:00:00 +0000

Seen: 13 times

Last updated: Feb 20 '23