Lists

Hi Friends,

In today’s discussion, we’ll see how to implement List. So, what is List? To answer this questions, Lists are like Arrays means index based collection + some bonus features like supporting adding and removing the elements from the collection. Below, in the snippet i have array of months containing months till November. However, i attempted to add the December in the collection and found the below error.

22nd

Since, arrays are readonly collection, it can’t grow or shrink. Hence, to fix this problem, Lists are introduced. Now, with a minimal change when i go ahead and run the same, then it will produce the expected output.

18th

However, one point to note here that List is a wrapper around array Array[T]. so, when we execute the List code, what we got is newly created list reference with the modified item in it. However, when List got created it always allocates extra blocks of memory to accommodate new incoming request to add the element in the List. Now, there are two main properties associated with List. They are

  • Count:- which gives the total no of elements present in the list.
  • Capacity:- gives the total no of count including extra block of memory to accommodate new ones.

Let’s do a quick demo around this to prove the point.

23rd

so, i can go ahead and add upto 16 items to the same list and its capacity won’t change, but, lets suppose that i add one more item to it like shown below, then Lists capacity will get reevaluated and then it will drop the existing internal array and create one new array with capacity 32 as shown below. Remember as i said Lists are wrapper around arrays. So, under the hood arrays are only working to hold the elements.

24th

However, this dynamic allocation of capacity could create performance issue when it comes to recreate the array and reference the same when there millions of item in the list, but if you are roughly sure about the list’s capacity well in advance, then you can use the pass the capacity size as shown below in the snippet.

25th

So, here i passed 20 as capacity, hence it produced the capacity 20 before and after same. Now, lets talk about remove operation. Below in the screen shot, intellisense has listed all the remove options available for the list.

26th

you can use either of the below mentioned option to remove the item. Both of these operations will do the same thing but the later one will be expensive, as it needs to scan all the elements of the list until it finds the required element. However, RemoveAt will simply jump on that index and remove the element. Even after removing the element, list will reorganize internally, so if the removal is the last element of the list, then it will be fastest operation and if the removal of the element is the top element, then it will be the slowest operation.

One more thing here is that you can extend the same to the readonly collection. Now, lets suppose that you want to pass your collection to some other code whom you don’t want this flexibility to modify your List. so, in order to achieve the same you can use the List with readonly collection as shown below.

Now, if i execute the same then it will throw the below exception. One more point to note here, that i am using IList here which means i can get or set the value based on index on the collection being passed.

27th

One point of caution here, even this code can be modified by using reflection. To circumvent we can modify the code a bit like shown below

Now, lets look at another collection and that is Collection. This provides an implementation of IList. Infact this is very similar to readonly collection, only change is you can modify the collection here. However, there is already good implementation of IList is there, then why Collection is required. To answer this question, List is very efficient but it can’t be customized. However, Collection on the other hand is specifically designed to be derived from, hence its highly customizable. Below, in the screen shot, you can see different virtual methods available to get overridden.

28th

so, here i can go ahead and implement any of these and test for specific checks like shown below.

29th

Now, lets look at the ObservableCollection. Now, this is one of the most famous collection to track the changes. As soon as list gets modified, it will send the notification.

Now, when i run the same it will produce me the below output showing that action Add added item December to the collection.

30th

With this i would like to end this List module here only. In the next section, we’ll pick some other module and discuss the same. Till then stay tuned and Happy Coding.

Thanks,
Rahul
Happy Coding

This entry was posted in C# and tagged . Bookmark the permalink.