What is yield return
It makes a method able to return sequence of values one at a time. The return type must be ienumerable or IEnumerator. It is memory efficient for large collection as lazy evaluation
It is better clean code than manually implementation of IEnumerator for lazy evaluation. Easy to return early and save performance
Better memory efficient than building collection and returning
Compiler transforms into state machine that pauses at yield and return then resume
Array vs ArrayList
Array is for collection of homogeneous types without fixed size.
Arraylist is for heterogeneous types with ability of dynamic sizing. It is slower due to boxing of value types. It is prone to runtime errors due to not having compile time type safety.
Array vs List
Array and list both hold same types with compile time type safety ensured. Both are stored in contiguous memory. Both preserve order
However array is fixed size. List allows dynamic sizing with special methods like add remove etc. These should be manually done in arrays
ArrayList vs List
List for homogeneous type safety
Arraylist for heterogeneous no type safety with boxing Unboxing overhead and is legacy
Non generic vs generic Collections
Boxing Unboxing of value type overhead
Prone to runtime cast errors
No compile time type safety
List vs Dictionary
Order not guaranteed. Hash based key lookup. Unique keys
Index access faster. Dictionary key access can slow down if key collision
Index insert is o(n) and Dictionary is 0(1) because it just have to drop in hash based bucket of keys
List light. Dictionary heavy
IEnumerable vs IQueryable
IEnumerable is root interface for all collections . It represents sequence. It operates on in memory collection within the running code with lazy evaluation. It has extension methods for linq to object
IQueryable extends IEnumerable to operate on Collections that are outside memory. It builds query expression trees for the data source provider and the data source executes it later as deferred execution. This way it can save network payload to get only filtered data. Delegate load to data source which is optimised for querying. It has extension for linq to entity
why arrays are still used when we have List<T></T>
Performance-critical scenarios
When size is known in advance
Lower memory overhead
Why use yield return instead of returning a List?
Saves memory for large sequences, supports lazy evaluation, and keeps code concise.
IEnumerator
It represents a cursor that helps foreach move forward the sequence. It has current property and movenext() and reset()
It converts foreach into state machine
Why use IEnumerable
Stream collection. No load on memory issues. Deffered execution. Just have to read
Evolution of collection Interfaces
IEnumerable is the root and represents a sequence. It provides read only and forward only iteration on it. It supports Deferred execution on in memory collection
ICollection extended IEnumerable to provide collection Manipulation features like count capacity contains and add remove clear
IList extends Icollection to provide index based fast access on o(1). It guarantees order preservation die to contiguous memory. Methods like insert and remove At
DICTIONARY extends collection to key value based collection with unique and no null key. Fast key lookup which is hash based. Order is not guaranteed
Set extends collection to give a set of unique elements for easy math operations. It is unordered collection
IQueryable extends IEnumerable to delegate the query execution to external provider. It supports Deferred execution on remote source. It builds expression tree and the source executes it in an optimized manner and the remaining result usually smaller than getting whole table saves on network bandwidth and response time
Where to use icollection
Session. Need to add remove count contains. No need for index access.
Or like blacklisting of refresh tokens. No need for order
Where to use IList
Playlist.
Reoder. Select a song fast.
Where to use Dictionary
Contact phone book.
Where to use set
Followers on social media
Where to use IQueryable
Don’t load millions of users. Query on db
Access times of list and Dictionary
O(1) for list
O(1) for key but o(n) if key collisions
Insert times for list and dictionary
O(n) for list
O(1) for dictionary because hash based drop
Capacity vs Count
Count is the actual no of elements
Capacity is size of internal array at present. It increases to almost double when resizing.
We can set a Capacity value to save on space if known. Because at declaration it is twice the size.
Why foreach throwing error
IENUMERABLE should not be modified while iteration
Difference between clear and new
Clear same capacity
New resets capacity
Release memory
Contains failing on objects
Because of reference equality match.
Override equals and gethashcode
Remove not removing everything
Only first occurrence
Returns true if found