Last active
May 24, 2024 17:46
-
-
Save priyankakundu/49d2b0afb03a99044e3772e60dd38ad1 to your computer and use it in GitHub Desktop.
Ienumerable vs List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IEnumerable describes behavior, while List is an implementation of that behavior. When you use IEnumerable, you give the compiler a chance | |
to defer work until later, possibly optimizing along the way. If you use ToList() you force the compiler to reify the results right away. | |
Whenever I'm "stacking" LINQ expressions, I use IEnumerable, because by only specifying the behavior I give LINQ a chance to defer evaluation and possibly optimize the program. Remember how LINQ doesn't generate the SQL to query the database until you enumerate it. | |
i.e deffered execution | |
- Non-Generic collections - These are the collections that can hold elements of different data types. It holds all elements as object type. | |
So it includes overhead of type conversions. | |
- Generic collections - These are the collections that can hold data of same type and we can decide what type of data that collections can hold. | |
Boxing: | |
int i = 123; | |
// The following line boxes i. | |
object o = i; | |
Unboxing: | |
o = 123; | |
i = (int)o; // unboxing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment