Today in this section, i’m going to talk about variety of C# collections. Here, in this course i’m going to cover below listed collections.
- Introduction to C# Collections
- Array Types
- Linked Lists, Stacks, Queues
- Multi Dimensional Arrays, Ranks and Bound
so, before we jump in and starts learning collection. we should stop a moment and think and think what a collection is. In, .Net Terminology “Collection means a type responsible for managing objects in memory and to provide access to those objects”. so, without wasting let’s gets started. we’ll 1st begin with Various collection definitions and then in the 2nd part we’ll start with the implementation.
- Lists:- In many scenarios order of the collection is very important, like consider a example of most popular movies of the year. so, in order to present this set we need to use that collection, which can present this set in ordered fashion, hence List is the ideal choice for that. Apart from that it also gives flexibility to access the element based on its index starting with index 0 and so on. Hence, this is also known as zero based indexing. Now, below I have mentioned List types in .Net T, List<T> , Collection<T>, ReadonlyCollection<T>, ObservableCollection<T>, IList<T>. Also one point to note here that index based collections are very efficient than other collections.
- Dictionaries:- A dictionary is a very different type of collection than list. Here, it’s not going to be referred by its index rather by name itself. so, name here actually means Key and key is nothing but the unique item in the dictionary and on that basis only it is going to be extracted from the dictionary. so, most widely used dictionary is of this signature Dictionary<TKey, TValue>. Now, the standard contract for Dictionary is defined by IDictionary<TKey, TValue>. Dictionaries are also pretty fast but Lists are having edge over dictionaries.
- Sets:- Sets are little different in concept from Lists and Dictionaries. With sets, we are not going to access individual element rather treating the whole collection as a single group. here, scenario could be to compare two sets and find the common elements present in both the sets. so, in that case we apply intersection of sets. similarly, for different scenarios if asked to apply union of sets, then we can also apply on these sets. Now, in .Net most common set collection is called HashSet<T>. Now, there is also an interface which defines standard contract for set collection and that is know as ISet<T>. One more thing to note here that there is similarity between Dictionaries and Sets as the underlying technology powering both is hashtable. However, sets don’t have a key.
Now, this is also important to understand that how collection operations work. 1st operation is reading from the collection and 2nd is writing to the collection. so, reading is quite simple like you might want to lookup a single element or might want to enumerate in the collection. So, for look ups we either provide index or key and the collection provides the requested items. Enumerating a collection is also very easy job in C#. It normally gets achieved by using foreach() loop. Now, when it comes to modify an item, there are two fundamental operations, like Add an Item or Remove an Item. these are only 2 meaningful operations for Dictionaries and Sets. However, for Lists, you can add item at a particular place as well.
In the 2nd module, we’ll begin delving each of these collections and their usages in details. Till then stay tuned and Happy coding.