Search code examples
c#performancesocketsbusiness-logic

Server performance - database access queries results: List<> or IEnumerable<>?


I am using the WebSocketSharp library to have a socket running on an ASP.NET server.

The server-side socket must access a database, thus it returns a collection of items.

Currently, I on the data access layer, methods return List<> types from the database:

async Task<List<object>> GetListOfItems();

I am wondering if an enumerable implementation would be faster and also cheap in terms of performance.

Also, should they be async calls aswell?


Solution

  • A List is an IEnumerable, via an intermediary - if you browse the Reference Source:

    public class List : IList, System.Collections.IList, IReadOnlyList

    And a IList is:

    public interface IList : ICollection

    And:

    public interface ICollection : IEnumerable

    So in effect they are the same when it comes to actually using them, and speed difference will depend on exactly what you are doing.

    If you want to know what is fastest, then test it! There is a Stopwatch class in System.Diagnostics for just that purpose.

    source: which is Faster? IEnumerable or List With Example.