I see lots of code bases throwing IList, List etc all over the place when half of the time all they really need is to return a set that can be either databound, or walked over and processed one at a time. For the scenarios where you don’t need to count the number of items or much of the other functionality that is exposed by the IList interface, you can start to leverage the yield keyword more to tighten up the code.
Here is a common example, a service layer method call that returns a list of DTO’s that can be consumed by some sort of binding target. Assume that you are mapping domain objects into DTO’s to be consumed by the upper level layer (which could then be mapped into a presentation model). Let’s also assume that you have the following
Customer Domain Class
ICustomerDTOMapper interface (implementation knows how to map from domain to dto).
ICustomerRepository - repository interface to find customers
CustomerTask - service layer class
Here is the CustomerTask class with its appropriate dependencies injected, and the existing GetAllCustomers method using the temporary list:
This is a small change, but handy nontheless. Again, this is not new information, I just think that more people could start taking advantage of yielding in more situations where full blown lists are not called for.