Could I return List from my DAL ?, In internet I read somewhere that it's not good. What is the problem if return List ?
2 Answers
You can return a List<T>
object, but the property should be typed IList<T>
instead. This will allow you to change the list implementation that you use at a later time, without breaking binary compatibility with existing assemblies compiled against your library.
This example probably won't match your constructor pattern, but it demonstrates what I mean (returning a List<T>
through an IList<T>
property):
public class Foo
{
public IList<string> AListOfStrings { get; private set; }
public Foo()
{
AListOfStrings = new List<string>();
}
}

- 158,093
- 24
- 286
- 300
-
2How about just returning `IEnumerable
`? That's even more flexible. – John Saunders Dec 14 '10 at 21:01 -
3@John: Perhaps, but I would favor `ICollection
` over `IEnumerable – cdhowie Dec 14 '10 at 21:02`. If this is a DAL class, modification may be necessary, and modifying IEnumerables is non-trivial, while `ICollection ` exposes mutating methods. -
1If you don't need indexing or count, you can use `IEnumerable
`, and then you would have more flexibility, like the option to return a lazy enumeration. – configurator Dec 14 '10 at 21:02 -
@cdhowie: `ICollection
` is making assumptions about how the collection will be used. I'd rather not do that. – John Saunders Dec 14 '10 at 21:05 -
@John: Same with `IEnumerable
`. Only the developer will be able to effectively choose which is appropriate. – cdhowie Dec 14 '10 at 21:07 -
@cdhowie: nonsense. `IEnumerable
` simply assumes it will be enumerated! An `ICollection – John Saunders Dec 14 '10 at 21:09` is something that can be modified. -
what about (sorry could not insert line breaks) , public ICollection
GetSchool() { ICollection – kayak Dec 14 '10 at 21:11schoolList = new List (); ..... return schoolList.ToList(); } -
@John: That's exactly my point. If the point is to expose a modifiable collection to the caller, `IEnumerable
` is not going to cut it. That's why the developer will have to choose which type makes the most sense for their application. – cdhowie Dec 14 '10 at 21:11 -
@kayak: There is no need to type `schoolList` as `ICollection
` -- you can use `List – cdhowie Dec 14 '10 at 21:12` there. And the `ToList()` call is unnecessary. Just return `schoolList`; `List ` implements `ICollection `, so no conversion is necessary. -
@cdhowie, Thanks i found some more links related to it http://stackoverflow.com/questions/400135/c-listt-or-ilistt – kayak Dec 14 '10 at 21:19
-
@cdhowie: it depends on what the DAL is trying to expose. I don't believe I've ever seen a DAL return a modifiable collection, unless it was planning to ignore any modifications, in which case I'd prefer it return `IEnumerable
` – John Saunders Dec 14 '10 at 22:35
In principle, there's no problem with returning List<T>
, however a common problem is when classes return a member List<T>
variable which violates encapsulation e.g.
public class SomeClass<T>
{
public List<T> Items { get; private set; }
public void DoSomething()
{
T first = Items[0];
...
}
}
In this case, any clients of this class can modify its state which could lead to bugs since the class can no longer protect its invariants. Note that returning IList<T>
in this case will not help.
For a data access object however, it will (hopefully) not retain the results of any queries, so a new copy of the list will be returned each time and it will not be affected by the modifications of any clients.

- 142,018
- 20
- 234
- 287
-
if any modifications to the list will be ignore anyway, why not return `IEnumerable
`. It seems more honest. – John Saunders Dec 14 '10 at 22:36