Collections in C#
The collection in C# is the collection of Data Structures. The collection consists of following data structures:
- ArrayList
- Stack
- SortedList
- Queue
- HashTable
These Data Structures are stored in a built-in library of C#. This library can be included in your program by.
1 |
Using System.Collection.Generic; |
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.
1 2 3 4 5 |
public int ID { get; set; } public string Name { get; set; } public int Pay { get; set; } |
Now to explain other functions, I will select on function for demonstration which is:
1 |
public int ID { get; set; } |
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:
1 2 3 4 5 6 7 8 9 |
Employee employee1 = new Employee(){ ID = 123, Name = "George", Pay = 15000 }; |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Employee employee2 = new Employee(){ ID = 456, Name = "jhon", Pay = 20000 }; Employee employee3 = new Employee(){ ID = 789, Name = "David", Pay = 30000 }; |
Stack in C#
Now I will define your Employee class as a stack data structure in the following manner.
1 |
Stack<Employee> StackEmployee = new Stack<Employee>(); |
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
1 |
StackEmployee.Push(employee1); |
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:
1 2 3 |
StackEmployee.Push(employee2); StackEmployee.Push(employee3); |
Display Using Pop Method in Stack
Now you can access and display the data sets of your Employee class in the following manner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Console.WriteLine("After Adding item in Stack "); foreach (Employee employee in StackEmployee) { //Displaying all data from the stack Console.WriteLine("Employee Id = {0} Employee Name = {1} EmployeePay ={2}" ,employee.ID ,employee.Name ,employee.Pay ); } Console.WriteLine("--------------------------------------------- "); Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); Console.WriteLine("------------------------------------------------"); Console.WriteLine("After Removing item in Stack "); StackEmployee.Pop(); // Pop method in stack foreach (Employee employee in StackEmployee) { Console.WriteLine("EmployeeId = {0} EmployeeName = {1} EmployeePay ={2}", employee.ID, employee.Name, employee.Pay); } Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); Console.WriteLine("----------------------------------------------"); Console.WriteLine("After Clear Stack "); StackEmployee.Clear();//this will clear all the data from the stack Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); Console.ReadLine(); }<strong> </strong> |
This statement Console.WriteLine(“After Adding item in Stack “); will simply display the sentence enclosed in quotation marks ” “.
foreach Loop to Display Stack Data
1 2 3 4 5 6 |
foreach (Employee employee in StackEmployee) { Console.WriteLine("Employee Id = {0} Employee Name = {1} EmployeePay ={2}" ,employee.ID ,employee.Name ,employee.Pay ); } |
1 |
foreach (Employee employee in StackEmployee){} |
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 {}.
1 |
Console.WriteLine("Employee Id = {0} Employee Name = {1} EmployeePay ={2}" ,employee.ID ,employee.Name ,employee.Pay ); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); Console.WriteLine("------------------------------------------------"); Console.WriteLine("After Removing item in Stack "); StackEmployee.Pop(); foreach (Employee employee in StackEmployee) { Console.WriteLine("EmployeeId = {0} EmployeeName = {1} EmployeePay ={2}", employee.ID, employee.Name, employee.Pay); } |
Count Method in Stack
1 |
Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); |
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.
1 |
StackEmployee.Pop(); |
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.
1 2 3 4 5 6 |
foreach (Employee employee in StackEmployee) { Console.WriteLine("EmployeeId = {0} EmployeeName = {1} EmployeePay ={2}", employee.ID, employee.Name, employee.Pay); } |
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
1 2 3 4 |
foreach (Employee employee in StackEmployee) { Console.WriteLine("EmployeeId = {0} EmployeeName = {1} EmployeePay ={2}", employee.ID, employee.Name, employee.Pay); } |
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:
1 |
Using System.Collection.Generic; |
Now, after that define your class in the following way:
1 2 3 4 5 6 7 8 9 10 11 |
class Employee { public int ID { get; set; } public string Name { get; set; } public int Pay { get; set; } } |
Now in your Main() do the following things:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
static void Main(string[] args) { int id; string name; int pay; int size; Console.WriteLine("ENTER the n.o Of Employees Whose Data You WANT To ENTER" ); Console.ReadLine(size); int []Employee employee= new int[size]; for (int i=0; i<size;i++) { Console.WriteLine("Enter the ID of Employee"i+1); Console.ReadLine(id); Console.WriteLine("Enter the Name of Employee"i+1); Console.ReadLine(name); Console.WriteLine("Enter the PAY of Employee"employee[i+1]); Console.ReadLine(pay); ID = id, Name = name, Pay = pay, }; Stack<Employee> StackEmployee = new Stack<Employee>(); for(int j=0;j<size;j++) { StackEmployee.Push(employee[j]); } foreach (Employee employee in StackEmployee) { Console.WriteLine("Employee Id = {0} Employee Name = {1} EmployeePay ={2}" ,employee.ID ,employee.Name ,employee.Pay ); } Console.WriteLine("--------------------------------------------- "); Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); Console.WriteLine("------------------------------------------------"); Console.WriteLine("After Removing item in Stack "); StackEmployee.Pop(); foreach (Employee employee in StackEmployee) { Console.WriteLine("EmployeeId = {0} EmployeeName = {1} EmployeePay ={2}", employee.ID, employee.Name, employee.Pay); } Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); Console.WriteLine("----------------------------------------------"); Console.WriteLine("After Clear Stack "); StackEmployee.Clear(); Console.WriteLine("Total item in Stack = " + StackEmployee.Count()); Console.ReadLine(); } |
Read more about Queue in C#