In this article, we will be talking about the collections namespace in the C# programming language. The collection namespace is usually used to manage data by storing it, refining it, manipulating it, and even sorting it. The collection namespace has several classes that can transform and sort data by using their different functions and methods for storing and refining data. The collection namespace is used to call these classes and utilize their functionalities for handling data while programming.
Classes of the Collections namespace:
When we use the System.Collections namespace in our C# program, we get access to the following list of classes to manipulate and accumulate data:
All these classes have some similarities and some differences when it comes to data handling. All of these are somewhat unique in their way of storing data and representing the data. We will discuss this one by one and also implement them in the Ubuntu 20.04 environment.
Array List Class in the C# programming language’s “Collections” Namespace:
Array List is an efficient way of storing data as it allows data to be sorted and dynamically allocated in the memory. It is very accessible to handle and search for data in an array list as it can be accessed through the individual index number of each element. The size of the array list is tangible and can be expanded according to the users need making it very suitable in uncertain situations. Now we will implement a simple array list in the C# programming language by using the system.collection namespace and get a better understanding of how it stores data.
In the above C# program, we have initialized the System.Collection namespace in the beginning to have access to the Array List class. Then, we created an object of the Array List class which will have access to the class’s functions. Then, we initialized some integer variables and assigned values to them. The object that we created of the Array list Class will be used to call the Add() function which will add the integer variables to the array list. In the end, we will use for each loop to look at how the integer variables are saved in the array list.
This is the order of the numbers in which they were added to the Array List and we can see that the list is tangible and can be expanded later on as well.
Stack Collection class in the C# programing language:
Stack is a data collection class in which the data is stored using the LIFO method which means Last in, First Out. The stack collection class is used when we need to get access to the last saved data first and modify or delete it. It has special functions to add and delete elements of the stack called Push() and Pop(). Both these functions will work on the last updated index of the stack as it runs in the LIFO Method. We will implement an example of the Stack data collection method in the Ubuntu 20.04 environment.
We are creating an object of the Stack class in this program to get access to the functions present in the stack class. The Push() function is then accessed by the object to add data to the Stack. We then printed the Stack list to get a view of how the data is saved.
As the output suggests, the last updated data is shown first which confirms the LIFO methodology of the Stack data collection.
Queue Class of the “Collections” namespace in C# programming language:
The data is stored in the order in which it was saved and updated. The special functions to add and delete data in this class are Enqueue() and Dequeue(). The current index of a queue is always the first data that was saved in the queue. Peek() is also a special function of the Queue class which displays the first added data of the queue. We will implement the concept of the queue in a C# program and display the list of data added to study the difference between Stack and Queue.
In this piece of code, we have called the System.Collection namespace to get access to the Queue class for storing data. We created an object of the Queue class and used it to use the Enqueue() function to add data to the queue. Then, we printed the queue by using the for each loop.
As the output shows, the data was displayed in the same order it was saved in as compared to Stack where it was the other way around.
Sorted List Class in the “Collections” Namespace of C# programming language:
A sorted list is a data collection type in which data is saved in pairs in a key and value relationship. The values are sorted based on the keys in ascending order. The sorted list can easily be accessed by the index of the List at any given time. The Add() and remove() functions are used to add and delete data from the list respectively. The Clear() function is also used to clear out all data from the list.
We have implemented the Sorted list in this C# program. First, we called the Collections namespace and then created an object of the Sorted list class to have access to its function. The object then saved the data in a pair form of keys and values. The first expression is the key while the second expression is the value. The data will be saved in the Dictionary Entry and with the help of the for-each loop, we will print the data of the Sorted List. After compiling and executing the above program we will get the output as shown in the snippet below:
As we can see that the Sorted List data is stored in ascending alphabetical order of the Key expression.
Hash table in the C# programming language’s “Collections” Namespace:
The hash table is a collection of data that stores the data in pairs like the sorted list. The hash table has a set of codes to accumulate a table. So, as a user we do not have to create a table at every moment. We can simply call the hash table collection and the table will be created itself. The hash table collection has several methods classified in it to add, delete, or manipulate data . Some of them are Add(), Clear(), ContainsKey() and ContainsValue(). We will implement this concept in the Ubuntu 20.04 environment.
In this C# program, we have first called the System.Collection namespace to get access to the Hash table and then created an object of this to call all the properties and data handling methods for our use. With that object, we used the Add() function to add data in the form of pairs of keys and values to the Hash table. As the paired data is saved in the dictionary entry, we will use this in for each loop to print the contents of the table.
This is how a hash table saves data in itself in the form of keys and values as represented in the above output screen.
Conclusion:
In this article, we discussed the Collections namespace in the C# programming language. The collections namespace has a bunch of data collection classes that store and accumulate data in different ways and methods to sort them. We discussed several of these classes that we use in our daily programming and also implemented them in the Ubuntu 20.04 environment.