Stack Collections in C#

Collections in C#

The collection in C# is the collection of Data Structures. The collection consists of following data structures:

  1. ArrayList
  2. Stack
  3. SortedList
  4. Queue
  5. HashTable

These Data Structures are stored in a built-in library of C#. This library can be included in your program by.

After that inside the brackets of Employee Class, I will have to define the get & set function in the following manner. First I have to define the functions in which I want to set the get & set functions in the following way:

Getters and Setters

As I have an Employee class so, the getters and setters will be.

Now to explain other functions, I will select on function for demonstration which is:

public means that this function is visible to all the classes related to the program and the data which is set in this function can be retrieved by all the other classes. The classes don’t need any permission to retrieve the data set in this function. int means that the return type of this function is an integer. It means that my function will return an Integer value. ID is the name of my function. { get; set; } shows that my function is setting data of ID by set function and getting data of ID by get function. All the other functions will work in the same manner.

As for this code, the data sets are pre-defined. So, I will define the data sets in Main() of my program as following.

Creating Instance of the Class

In the {} brackets of (static void Main(string[] args){}) function first I will make the instance of my Employee class in the following manner:

employee1 is the instance of Employee class and = new Employee() will set the employee data via employee1 instance in the Employee class. Now ID = 123, means that 123 is set as data in ID and so, is the case with variables Name & Pay.

You can set multiple instances of Employee class in the same manner as

Stack in C#

Now I will define your Employee class as a stack data structure in the following manner.

Stack<Employee> means that Employee class will be processed as a Stack class, and all the features of Stack will be assigned to this class. StackEmployee is the instance of the class Employee of stack type. new Stack<Employee>(); will allow data sets to be saved in the Employee class in the form of a  Stack.


Push() Method in Stack

Push() is a built-in function in C# which can be used once the library of Collection is included in your program. The. Push(employee1); will push the data set of the employee1 instance, in the StackEmployee instance of  Employee class. Similarly, you can push multiple data sets in the form of a stack in Employee class which are as follows:


Display Using Pop Method in Stack

Now you can access and display the data sets of your Employee class in the following manner.

This statement  Console.WriteLine(“After Adding item in Stack “); will simply display the sentence enclosed in quotation marks ” “.

foreach Loop to Display Stack Data

foreach () loop will perform the same operations on StackEmployee (the stack instance of a class (Note: That employee is a general instance of class Employee whereas StackEmployee is the stack instance of Employee class )) enclosed in {}.

Console.WriteLine will print the data set of one instance of the Employee class suppose employee1 data set with the foreach loop. The starting and ending limit for foreach loop depends on the number of data sets defined by you or the number of instances defined by you. As you have made three instances of your Employee class as employee1, employee2, employee3. So, the foreach loop will run three times.


Count Method in Stack

Count() is a built-in function in C# which counts the number of data sets or data elements in your program. StackEmployee.Count() means that Count() will count the number of StackEmployee instances of your Employee class.

Pop() is a built in function in C# which is included in the Collection library in C#. Now Pop() will remove one StackEmployee instance of your Employee class.

Those above-mentioned statements after the foreach loop will show the contents of the datasets when one data set is removed by the Pop() function.

When again this statement is executed StackEmployee.Pop(); then one more data set will be removed from your stack will be minimized by one data set more.

After this, when you type this statement Console.WriteLine(“Total item in Stack = ” + StackEmployee.Count()); then the Count() function will count the total number of data sets after the removal of one more data set.

Again, when you type those set of statements

Then again the contents of the remaining data sets will be shown to you.

Another Scenario:

Now suppose that you don’t have a pre-defined number of data sets, and you don’t know about the contents of those data sets. In this scenario, you will code in the following manner.

First, you have to create an Employee class defining your data sets in the following manner:

First Add an Employee class by the method mentioned above with your program. Then in this Employee class include the library of Collection in the following way:


Now, after that define your class in the following way:


Now in your Main() do the following things:


Read more about Queue in C#

Leave a Reply

Your email address will not be published. Required fields are marked *