Understanding of interfaces

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.

+5
source share
11 answers

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 .

+4
source

@ Jason

You can also return IList <>, because the array really implements this interface.

+2
source

- - , , , generics, List <Employee > .

, ArrayList , , , LinkedList, - , , (.. " ArrayList())".

+1

, , , , IEnumerable ( .NET).

, , , // , IEnumerable, , , - , .

+1

- , ? IEnumerable<Employee>? , - , , - , .

+1

, . , , .

. ArrayList, , . IEnumerable, , .

, , . IEnumerable, , , . , , , , , .

, ArrayList. .

+1

IList<Employee>.

, , IList, , ArrayList. , - , LinkedList YourCustomSuperDuperList , .

101.; -)

0

- .

, , , .

, , .

0

, , - .NETish, IList, List ArrayList, .NET.

0

- , , , ; , , , , "" .

, , , , ArrayList : list.toArray()

0

, , , , . List . FXCop.

0

All Articles