I am wondering about the following fact. I have a data repository that returns all my data of IEnumerable<Customer>.
In my business logic sometimes I need lists so I can add stuff for example.
When I retrieve the IEnumerable<Customer> I have 2 options to get a list from it.
Either using the Linq extension method .ToList() or cast it (I think its not a conversion) like this (List<Customer>)IEnumerable<Customer>.
Must mention that I don’t use the list for iterations so I don’t need a new copy of my enumeration each time. In this case is it true that in my simple case I must use the cast method instead of .ToList (which creates a new copy) ?
// use simple cast?
List<Customer> customers = (List<Customers>)DataSource.GetCustomers();
// or if i use this i get a bit of performance loss?
List<Customer> customers = DataSource.GetCustomers().ToList();
The whole point of your repository returning an IEnumerable is that it isn’t guaranteed to actually be a list. (It may be now, but the use of IEnumerable allows the implementation to be changed later).
Either go with the .ToList() method, or make the repository return a List or IList instead.