I have a class method that returns a list of employees with whom I can iterate. What is the best way to return a list? I usually just return an ArrayList. However, as I understand it, interfaces are better for this type of action. What would be the best interface to use? Also, why is it better to return an interface rather than an implementation (say, an ArrayList object)? It just seems to me that I have a lot more work.
Personally, I used List <Employee> to create a list on the backend, and then use IList when you return. When you use interfaces, it gives flexibility in changing the implementation without having to change who uses your code. If you want to stick with an ArrayList, this will not be a generic IList .
@ Jason
You can also return IList <>, because the array really implements this interface.
- - , , , generics, List <Employee > .
<Employee
, ArrayList , , , LinkedList, - , , (.. " ArrayList())".
, , , , IEnumerable ( .NET).
, , , // , IEnumerable, , , - , .
- , ? IEnumerable<Employee>? , - , , - , .
IEnumerable<Employee>
, . , , .
. ArrayList, , . IEnumerable, , .
, , . IEnumerable, , , . , , , , , .
, ArrayList. .
IList<Employee>.
IList<Employee>
, , IList, , ArrayList. , - , LinkedList YourCustomSuperDuperList , .
IList
ArrayList
LinkedList
YourCustomSuperDuperList
101.; -)
- .
, , , .
, , .
, , - .NETish, IList, List ArrayList, .NET.
- , , , ; , , , , "" .
, , , , ArrayList : list.toArray()
, , , , . List . FXCop.