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.
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.
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.
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.
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.
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.
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.
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
so, here i can go ahead and implement any of these and test for specific checks like shown below.
Now, lets look at the ObservableCollection
Now, when i run the same it will produce me the below output showing that action Add added item December to the collection.
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.