Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which interface should I expose a List<T> via?

In a response to this question runefs suggested that "unless you have a very specific reason for using IList you should considere IEnumerable". Which do you use and why?

like image 642
ng5000 Avatar asked Nov 28 '22 12:11

ng5000


1 Answers

IEnumberable<T> is read-only, you have to reconstruct the collection to make changes to it. On the other hand, IList<T> is read-write. So if you expect a lot of changes to the collection, expose IList<T> but if it's safe to assume that you won't modify it, go with IEnumerable<T>.

like image 125
Tamas Czinege Avatar answered Dec 07 '22 20:12

Tamas Czinege