Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Interfaces

Tags:

interface

I have class method that returns a list of employees that I can iterate through. What's the best way to return the list? Typically I just return an ArrayList. However, as I understand, interfaces are better suited for this type of action. Which would be the best interface to use? Also, why is it better to return an interface, rather than the implementation (say ArrayList object)? It just seems like a lot more work to me.

like image 332
Aaron Sanders Avatar asked Aug 27 '08 18:08

Aaron Sanders


2 Answers

Personally, I would use a List<Employee> for creating the list on the backend, and then use IList when you return. When you use interfaces, it gives you the flexability to change the implementation without having to alter who's using your code. If you wanted to stick with an ArrayList, that'd be a non-generic IList.

like image 149
bdukes Avatar answered Sep 29 '22 23:09

bdukes


@ Jason

You may as well return IList<> because an array actually implements this interface.

like image 33
Quibblesome Avatar answered Sep 29 '22 21:09

Quibblesome