IEnumerable vs List - What to Use? How do they work?

后端 未结 10 2145
臣服心动
臣服心动 2020-11-22 03:57

I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects:

List sel = (from animal in Animals 
                         


        
10条回答
  •  醉酒成梦
    2020-11-22 04:33

    If all you want to do is enumerate them, use the IEnumerable.

    Beware, though, that changing the original collection being enumerated is a dangerous operation - in this case, you will want to ToList first. This will create a new list element for each element in memory, enumerating the IEnumerable and is thus less performant if you only enumerate once - but safer and sometimes the List methods are handy (for instance in random access).

提交回复
热议问题