So the count of total items can be retrieved in O(n) time. On the other hand, If we need to update or delete anything of any item, we can not by IEnumerable and If we need to count how many data we have in collection then we have to iterate each element to get count. Assume that you have a lot of records so IEnumerable puts overhead on your memory. ![]() It will bring ALL data from server to client, then filters them. ListOfEmployee = listOfEmployee.Where(x => x.Id > 70).ToList() ListOfEmployee = From employee in ctx.Employees If we need to build case study 2 API and we are going to use IEnumerable then what will happen let's see var ctx = new DataAccessEntities() Now I can explain it properly for clear understanding. An IEnumerable does not hold even the count of the items in the list, instead, you have to iterate over the elements to get the count of items. It is the most basic type of list container. In the IEnumerable, you can iterate through each element and you cannot modify (Add or Update or Remove) data. IEnumerableĪn IEnumerable is a list or container which can hold some items. I will explain it by case study like Employee and explain all the things including time and space complexity for clear understanding about collections.Ĭase study 1 : Get all employees (Without modification anything of items).Ĭase study 2 : Get all employees whose Id greater than 70.Ĭase study 3 : Get all employees (Some modification of items in client side). ![]() I will explore which one should we use in which case step by step and clear comparison with all of them with real life example. Collections in c#, we have been using it interchangeably but do we really know what is it that separates each other.They all have specific characteristics that differentiate them and makes them adaptable to certain scenarios.
0 Comments
Leave a Reply. |