If you have worked with IEnumerable and using ReSharper, then you
may land up into this warning, "Possible Multiple Enumeration of IEnumerable". So, what this warning is all about?
Well, before proceeding further, let’s see how we can store
something into IEnumerable object .
IEnumerable items = GetAllItems()
So, above code itself speaks that items is a variable of
type IEnumerable which will hold some values/objects we can iterate through.
Now here lies the performance hit, which is also indicated in the form of ReSharper warning. This performance hit may not be
significant for a small number of items. But this can be noticed while dealing
with a huge number of items.
Reason behind this is, whenever you are iterating through
items collection, GetAllItems method will be called for the same number of
times.
Solution: It is always good to materialized the result in a
list or array like below:
IEnumerable items = GetAllItems().ToList()
Once you are done with mentioned code changes, you will notice that above warning doesn't exist any more.
Once you are done with mentioned code changes, you will notice that above warning doesn't exist any more.
Hope you like this small post.
Comments
Post a Comment