SE OOP Lecture14
SE OOP Lecture14
BS – (SOFTWARE ENGINEERING)
LECTURE # 14
1
Contents
2
Collections
Prioritized Collections
Specialized Collections
Hashtable Class
SortedList Class
Stack Class
Queue Class
3
What is a Collection in C#?
4
So in simple words, we can say a Collection in C# is a dynamic array. That means the collections in C# have
the capability of storing multiple values but with the following features.
Size can be increased dynamically.
It also provides the facility to remove or delete elements from the middle of a collection.
The collections in C# are classes that represent a group of objects. With the help of C# Collections, we can
perform different types of operations on objects such as Store, Update, Delete, Retrieve, Search, and Sort
objects, etc. In short, all the data structure work can be performed by collections in C#. That means
Collections standardize the way in which the objects are handled by our program.
Collections are nothing but groups of records that can be treated as one
logical unit. For example, you can have a country collection that can have
a country code and country name. You can have a product collection that
has the Product Id and Product Name. You can have an employee
collection having the employee’s name and employee id. You can have a
book collection having an ISBN number and book name. For a better
understanding, please have a look at the below image.
Note: So, basically, if we want to fetch the records based on a key, then
we need to use Key-Value Pair collections such as Hashtable,
Dictionary, and SortedList.
• In simple words, we can say that the Arrays in C# are the simple
data structure that is used to store similar types of data items in
sequential order. Although the arrays in C# are commonly used,
they have some limitations.
• For example, you need to specify the array’s size while creating the
array. If at execution time, you want to modify it that means if you
want to increase or decrease the size of an array, then you need to
do it manually by creating a new array or by using the Array class’s
Resize method, which internally creates a new array and copies the
existing array element into the new array.
The array size is fixed. Once the array is created we can never increase the
size of the array. If we want then we can do it manually by creating a new
array and copying the old array elements into the new array or by using the
Array class Resize method which will do the same thing means to create a
new array and copy the old array elements into the new array and then
destroy the old array.
We can never insert an element into the middle of an array
System.Collections.Generic classes
System.Collections.Concurrent classes
The Non-Generic Collection Classes come with C# 1.0 and are defined under
the System.Collections namespace. The Non-Generic collection classes in C#
operate on objects, and hence can handle any type of data, but not in a safe-type
manner. The System.Collections namespace contains the following classes:
ArrayList: It Implements the System.Collections.IList interface using an array whose size is
dynamically increased as required.
Hashtable: It represents a collection of key/value pairs that are organized based on the hash code
of the key.
SortedList: It represents a collection of key/value pairs that are sorted by the keys and are
accessible by key and by index.
The Generic Collection Classes come with C# 2.0 and are defined under
structures like linked lists, stacks, queues, and dictionaries. These collection classes are type-safe because
they are generic means only those items that are type-compatible with the type of the collection can be
List<T>: It represents a strongly typed list of objects that can be accessed by index. Provides
Stack<T>: It represents a variable size last-in-first-out (LIFO) collection of instances of the same
specified type.
HashSet<T>: It represents a set of values. It removes duplicate elements from the collection.
SortedList<TKey, TValue>: It represents a collection of key/value pairs that are sorted by key
on the key.
It came in .NET Framework Version 4 and onwards. It provides various threads-safe collection classes
classes for thread-safe operations. Now multiple threads will not create problems for accessing the
BlockingCollection<T>: It provides blocking and bounding capabilities for thread-safe collections that
implement System.Collections.Concurrent.IProducerConsumerCollection.
20
ArrayList Class
21
be indexed individually.
The following table lists some of the commonly used properties of the
ArrayList class −
Capacity -- Gets or sets the number of elements that the ArrayList can contain.
Count -- Gets the number of elements actually contained in the ArrayList.
IsFixedSize -- Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly -- Gets a value indicating whether the ArrayList is read-only.
Item -- Gets or sets the element at the specified index.
Returns an ArrayList which represents a subset of the elements in the source ArrayList.
Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
Inserts the elements of a collection into the ArrayList at the specified index.
The following table lists some of the commonly used properties of the Hashtable class
Count
IsFixedSize
IsReadOnly
Item
Keys
Values
The following table lists some of the commonly used methods of the Hashtable class
Adds an element with the specified key and value into the Hashtable.
Removes the element with the specified key from the Hashtable.
The following table lists some of the commonly used properties of the SortedList
class −
IsFixedSize - Gets a value indicating whether the SortedList has a fixed size.
Item - Gets and sets the value associated with a specific key in the SortedList.
The following table lists some of the commonly used methods of the SortedList class −
public virtual void Add(object key, object value); -- Adds an element with the specified key and
public virtual void Clear(); -- Removes all elements from the SortedList.
public virtual bool ContainsKey(object key); -- Determines whether the SortedList contains a
specific key.
public virtual bool ContainsValue(object value); -- Determines whether the SortedList contains a
specific value.
public virtual object GetByIndex(int index); -- Gets the value at the specified index of the
SortedList.
public virtual object GetKey(int index); -- Gets the key at the specified index of the SortedList.
public virtual int IndexOfKey(object key); -- Returns the zero-based index of the
specified key in the SortedList.
public virtual int IndexOfValue(object value); -- Returns the zero-based index of the
first occurrence of the specified value in the SortedList.
public virtual void Remove(object key); -- Removes the element with the specified key
from the SortedList.
public virtual void RemoveAt(int index); -- Removes the element at the specified
index of SortedList.
public virtual void TrimToSize(); -- Sets the capacity to the actual number of elements
in the SortedList.
when you need a last-in, first-out access of items. When you add
an item in the list, it is called pushing the item and when you
The following table lists some of the commonly used methods of the Stack class
public virtual void Clear(); -- Removes all elements from the Stack.
public virtual object Peek(); -- Returns the object at the top of the Stack without
removing it.
public virtual object Pop(); -- Removes and returns the object at the top of the Stack.
public virtual void Push(object obj); -- Inserts an object at the top of the Stack.
you add an item in the list, it is called enqueue, and when you
public virtual bool Contains(object obj); -- Determines whether an element is in the Queue.
public virtual object Dequeue(); -- Removes and returns the object at the beginning of the
Queue.
public virtual void Enqueue(object obj); -- Adds an object to the end of the Queue.
public virtual void TrimToSize(); -- Sets the capacity to the actual number of elements in the
Queue.
THANK YOU.
44