public void WorkingWithArrayList() { //the members of any type in System.Collections are designed to work with type 'Object' //hence even if you insert a value type in these collections a performance hit is encountered due to boxing //to top it all its not type safe, i can insert anything in the below ArrayList //the only option left in this case is to create a custom type-safe collection class arrList.Add(10); arrList.Add(20); arrList.Add(30); //arrList.Add(40.34); foreach (Int32 item in arrList) { Console.Write(item + " "); } Employee emp1 = new Employee(48090, "naynish", 85000); Employee emp2 = new Employee(48091, "purab", 65000); Employee emp3 = new Employee(48092, "jay", 75000); //not inserted in the Hash Table Employee emp4 = new Employee(48093, "tripti", 60000); //Add these Employee objects to the custom collection EmployeeObjectCollection collection = new EmployeeObjectCollection(); collection.Add(emp1); collection.Add(emp2); collection.Add(emp3); int key = 48095; if (collection.GetValue(key).Id != 0) { Console.WriteLine(collection.GetValue(key)); } else { Console.WriteLine("\nEmployee {0} does not exist in the Hash Table", key); Console.WriteLine("please view the below hash table entries and enter the correct key"); foreach (DictionaryEntry item in collection) { Console.WriteLine((Employee)item.Value); } } }
public Employee GetValue(int e) { Employee temp = new Employee(); if (htEmployee.ContainsKey(e)) { foreach (DictionaryEntry item in htEmployee) { if (e.ToString() == item.Key.ToString()) { temp = (Employee)item.Value; } } } return temp; }
public void Remove(Employee e) { htEmployee.Remove(e.Id); }
//now my Add is prototyped to accept only Employee objects //if you try to pass in any other type then you will receive a compile time error public void Add(Employee e) { htEmployee.Add(e.Id, e); }
public void WorkingWithStack() { Console.WriteLine("\nstack implementation"); Stack s = new Stack(); Employee emp1 = new Employee(90, "hello world!", 45000); Employee emp2 = new Employee(91, "test data", 90000); Employee emp3 = new Employee(92, "android", 95000); s.Push(emp1); s.Push(emp2); s.Push(emp3); s.Pop(); foreach (Employee item in s) { Console.WriteLine(item); } }
public void WorkingWithSortedList() { Console.WriteLine("\nsorted list implementation"); SortedList s = new SortedList(); Employee emp1 = new Employee(90, "hello world!", 45000); Employee emp2 = new Employee(91, "test data", 90000); Employee emp3 = new Employee(92, "android", 95000); s.Add(emp1.Id, emp1); s.Add(emp2.Id, emp2); s.Add(emp3.Id, emp3); s.Remove(emp2.Id); foreach (DictionaryEntry item in s) { Console.WriteLine("key {0}, value {1}", item.Key, item.Value); } }
public void WorkingWithQueue() { Console.WriteLine("\nqueue implementation"); Queue q1 = new Queue(); Employee emp1 = new Employee(90, "hello world!", 45000); Employee emp2 = new Employee(91, "test data", 90000); Employee emp3 = new Employee(92, "android", 95000); q1.Enqueue(emp1); q1.Enqueue(emp2); q1.Enqueue(emp3); q1.Dequeue(); Console.WriteLine("Queue element count {0}",q1.Count); foreach (Employee item in q1) { Console.WriteLine(item); } }