<p>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.</p>

[{'answer_id': 30771, 'author': 'bdukes', 'author_id': 2688, 'author_profile': 'https://Stackoverflow.com/users/2688', 'pm_score': 3, 'selected': True, 'text': '<p>Personally, I would use a <a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" rel="nofollow noreferrer">List</a>&lt;Employee> for creating the list on the backend, and then use <a href="http://msdn.microsoft.com/en-us/library/5y536ey6.aspx" rel="nofollow noreferrer">IList</a> 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 <a href="http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx" rel="nofollow noreferrer">IList</a>.</p>\n'}, {'answer_id': 30774, 'author': 'levand', 'author_id': 3044, 'author_profile': 'https://Stackoverflow.com/users/3044', 'pm_score': 1, 'selected': False, 'text': '<p>The best way to do something like this would be to return, as you say, a List, preferably using generics, so it would be List<code>&lt;Employee</code>>.</p>\n\n<p>Returning a List rather than an ArrayList means that if later you decide to use, say, a LinkedList, you don\'t have to change any of the code other than where you create the object to begin with (i.e, the call to "new ArrayList())". </p>\n'}, {'answer_id': 30780, 'author': 'Marcin', 'author_id': 3105, 'author_profile': 'https://Stackoverflow.com/users/3105', 'pm_score': 0, 'selected': False, 'text': "<p>Return type for your method should be <code>IList&lt;Employee&gt;</code>. </p>\n\n<p>That means that the caller of your method can use anything that <code>IList</code> offers but cannot use things specific to <code>ArrayList</code>. Then if you feel at some point that <code>LinkedList</code> or <code>YourCustomSuperDuperList</code> offers better performance or other advantages you can safely use it within your method and not screw callers of it.</p>\n\n<p>That's roughly interfaces 101. ;-)</p>\n"}, {'answer_id': 30783, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 0, 'selected': False, 'text': '<p>An interface is a contract between the implementation and the user of the implementation.</p>\n\n<p>By using an interface, you allow the implementation to change as much as it wants as long as it maintains the contract for the users.</p>\n\n<p>It also allows multiple implementations to use the same interface so that users can reuse code that interacts with the interface.</p>\n'}, {'answer_id': 30785, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 0, 'selected': False, 'text': "<p>You don't say what language you're talking about, but in something .NETish, then it's no more work to return an IList than a List or even an ArrayList, though the mere mention of that obsolete class makes me think you're not talking about .NET.</p>\n"}, {'answer_id': 30786, 'author': 'Quibblesome', 'author_id': 1143, 'author_profile': 'https://Stackoverflow.com/users/1143', 'pm_score': 2, 'selected': False, 'text': '<p>@ Jason</p>\n\n<p>You may as well return IList&lt;> because an array actually implements this interface.</p>\n'}, {'answer_id': 30794, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 1, 'selected': False, 'text': '<p>If all you are doing is iterating through the list, you can define a method that returns the list as IEnumerable (for .NET).</p>\n\n<p>By returning the interface that provides just the functionality you need, if some new collection type comes along in the future that is better/faster/a better match for your application, as long as it still implements IEnumerable you can completely rewrite your method, using the new type inside it, without changing any of the code that calls it.</p>\n'}, {'answer_id': 30799, 'author': 'mmattax', 'author_id': 1638, 'author_profile': 'https://Stackoverflow.com/users/1638', 'pm_score': 0, 'selected': False, 'text': '<p>An interface is essentially a contract that a class has certain methods or attributes; programming to an interface rather then a direct implementation allows for more dynamic and manageable code, as you can completely swap out implementations as long as the "contract" is still held.</p>\n\n<p>In the case you describe, passing an interface does not give you a particular advantage, if it were me, I would pass the ArrayList with the generic type, or pass the Array itself: list.toArray()</p>\n'}, {'answer_id': 30819, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 1, 'selected': False, 'text': '<p>Is there any reason the collection needs to be ordered?  Why not simply return an <code>IEnumerable&lt;Employee&gt;</code>?  This gives the bare minimum that is required - if you later wanted some other form of storage, like a Bag or Set or Tree or whatnot, your contract would remain intact.</p>\n'}, {'answer_id': 30822, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 0, 'selected': False, 'text': '<p>Actually you shouldn\'t return a List if thats a framework, at least not without thinking it, the recommended class to use is a Collection. The List class has some performance improvements at the cost of server extendability issues. It\'s in fact an FXCop rule.</p>\n\n<p>You have the reasoning for that in <a href="http://blogs.msdn.com/fxcop/archive/2006/04/27/585476.aspx" rel="nofollow noreferrer">this article</a></p>\n'}, {'answer_id': 30825, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': "<p>I disagree with the premise that it's better to return an interface.  My reason is that you want to maximize the usefulness a given block of code exposes.  </p>\n\n<p>With that in mind, an interface works for accepting an item as an argument.  If a function parameter calls for an array or an ArrayList, that's the only thing you can pass to it.  If a function parameter calls for an IEnumerable it will accept either, as well as a number of other objects.  It's more useful</p>\n\n<p>The return value, however, works opposite.  When you return an IEnumerable, the only thing you can do is enumerate it.  If you have a List handy and return that then code that calls your function can also easily do a number of other things, like get a count.</p>\n\n<p>I stand united with those advising you to get away from the ArrayList, though.  Generics are <em>so much better.</em></p>\n"}]
['https://Stackoverflow.com/questions/30763', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2121/']