Home >Backend Development >C++ >IEnumerable vs. List in C#: When Should I Use Which for Optimal Performance?
C#collection: comparison of the performance of IenumerabLE and List and the best practice
When processing the set in C#, select
or is essential for performance and efficiency. Although both represent the element sequence, the implementation method is very different. IEnumerable
List
is an interface that defines the behavior of the element sequence. It provides the operation of traversal sequence elements, but does not need to load the entire sequence to memory. This allows high -efficiency to handle large data sets because data is delayed.
List: The specific collection class IEnumerable
Use scene
Choose or List
, please consider the following guidelines:
Using Situation:
Need to handle large datasets and delay processing.
IEnumerable
Want to avoid the overhead of the sequence to the memory. List
IEnumerable
List
Consider the following linq query:
Use to allow delay to find value query. If is used to specifically result, it will mandate the entire sequence to the memory, which may affect performance. IEnumerable
List
More resources IEnumerable
MSDN document
<code class="language-csharp">IEnumerable<animal> sel = (from animal in Animals join race in Species on animal.SpeciesKey equals race.SpeciesKey select animal).Distinct();</code>MSDN document
IEnumerable
ToList()
The above is the detailed content of IEnumerable vs. List in C#: When Should I Use Which for Optimal Performance?. For more information, please follow other related articles on the PHP Chinese website!