-
[C#] IEnumerable vs ListC#/기초 2022. 6. 27. 15:07
C# IEnumerable vs List and Array
When you should use one over the other and what I learned trying to work this out…
TL;DR
It depends! As is often the case with programming.
IEnumerables delay execution of the query until the code is looped over or a value type is extracted. Basically IEnumerables have a method to get the next item in the collection, so they look at items “one at a time”, they don’t need the whole collection to be in memory and don’t know how many items are in it, foreach just keeps getting the next item until it runs out. IEnumerables also help ensure immutability, as you are always querying the source there are no unintended side effects.
Lists and Arrays create objects in memory and allow access to a whole lot of methods associated with those types ( Lists | Arrays ). Lists and Arrays know how many items are in the collection and have more acknowledgment of their whole overall structure.
When you might use an IEnumerable:
- A massive database table, you don’t want to copy the entire thing into memory and cause performance issues in your application.
When you might use a List or Array:
- You need the results right away and are possibly mutating the structure you are querying later on.
https://medium.com/@ben.k.muller/c-ienumerable-vs-list-and-array-9f099f157f4f
C# IEnumerable vs List and Array
When you should use one over the other and what I learned trying to work this out…
medium.com
728x90'C# > 기초' 카테고리의 다른 글
[C#] 문법 (0) 2022.06.29 [C#] ctor 자동생성 (0) 2022.06.28 [C#]배열 초기화 (0) 2022.06.15 [C#]Nuget (0) 2022.04.08 [C#] abstract, virtual, override (0) 2022.04.04 댓글