-2

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?

k1dev
  • 631
  • 5
  • 14
  • According to this answer, yes you can/should async. https://stackoverflow.com/questions/23295119/asynchronous-iterator-taskienumerablet – Any Moose Aug 16 '18 at 18:39
  • @AnyMoose that doesn't reply my question – k1dev Aug 16 '18 at 18:42
  • you asked "If so, should them be async calls aswell?". so i answered. You might need to simplify your question to get better answers. – Any Moose Aug 16 '18 at 18:43
  • @AnyMoose I need to do async socket-sent messages on each item returned by `Manager.GetListOfItems()`, which is unknown if it should return either `List` or `Enumerable`, basically whichever is faster and cheaper. Post an answer and I will accept if it suits my needings – k1dev Aug 16 '18 at 18:45

1 Answers1

1

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.

Any Moose
  • 723
  • 6
  • 12