0% found this document useful (0 votes)
21 views44 pages

SE OOP Lecture14

Uploaded by

Ali Uskuplo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views44 pages

SE OOP Lecture14

Uploaded by

Ali Uskuplo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

OBJECT ORIENTED PROGRAMMING

BS – (SOFTWARE ENGINEERING)

FALL SEMESTER - 2023

LECTURE # 14

COURSE INSTRUCTOR: AMJAD ALI KHAWAJA

1
Contents
2

 Collections

 General Categories of Collections


 Indexed Base Collections

 Key-Value Pair Collections

 Prioritized Collections

 Specialized Collections

 What are Array and their Disadvantages in C#?

 Following are the Limitations of Array in C#:

 Types of Collections NameSpace in C#


 Non-Generic Collections Classes in C#

 Generic Collections Classes in C#

 Concurrent Collection Classes in C#

 Non-Generic Collections Classes


 ArrayList Class

 Hashtable Class

 SortedList Class

 Stack Class

 Queue Class

Object Oriented Programming Fall-2023


Any Queries From
Previous Lectures

3
What is a Collection in C#?
4

 The Collections in C# are a set of predefined classes that are present in


the System.Collections namespace that provides greater capabilities and functionalities than the traditional
arrays. The collections in C# are reusable, more powerful, and more efficient and most importantly they have
been designed and tested to ensure quality and performance.

 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.

 We can insert an element into the middle of a collection.

 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.

Object Oriented Programming Fall-2023


Collections
5

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.

Object Oriented Programming Fall-2023


General Categories of Collections
6

Collections are classified into 4 types such as Indexed


Based, Key-Value Pair, Prioritized Collection, and
Specialized Collections.

Object Oriented Programming Fall-2023


General Categories of Collections:
7

Object Oriented Programming Fall-2023


Indexed Base Collections
8

In Indexed Based, we have two kinds of collections i.e.


Array and List. So, when we add any elements to .NET
Collections like Array, List, or ArrayList, it maintains
its own internal index number. This internal index
number is auto-generated by the framework and using
this index number we can identify the records.

Object Oriented Programming Fall-2023


Key-Value Pair Collections
9

In the Key-Value Pair collection, we have Hashtable, Dictionary, and


SortedList. In real-time projects, we rarely accessed the records using
the internal index numbers. We generally use some kind of keys to
identify the records and there we use the Key-Value Pair collections
like Hashtable, Dictionary, and SortedList.

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.

Object Oriented Programming Fall-2023


Prioritized Collections
10

The Prioritized Collections help you to access the elements in a particular


sequence. The Stack and Queue collections belong to the Prioritized
Collections category. If you want First in First Out (FIFO) access to the
elements of a collection, then you need to use Queue collection. On the
other hand, if you want Last in First Out (LIFO) access to the elements of
a collection, then you need to use the Stack collection. For a better
understanding, please have a look at the below image.

Object Oriented Programming Fall-2023


Specialized Collections
11

 The Specialized Collections are specifically designed

for a specific purpose. For example, a Hybrid


Dictionary starts as a list and then becomes a
hashtable.

Object Oriented Programming Fall-2023


What are Array and Their disadvantages in C#?
12

• 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.

Object Oriented Programming Fall-2023


Following are the Limitations of Array in C#
13

 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

 Deleting or removing elements from the middle of the array.

 To overcome the above problems, the Collections are introduced in C# 1.0.

Object Oriented Programming Fall-2023


Types of Collections NameSpace in C#
14

There are 3 ways to work with collections. The three


namespaces are given below:
 System.Collections classes

 System.Collections.Generic classes

 System.Collections.Concurrent classes

Object Oriented Programming Fall-2023


Types of Collections NameSpace in C#
15

Object Oriented Programming Fall-2023


Non-Generic Collections Classes in C#
16

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.

 Stack: It represents a simple last-in-first-out (LIFO) non-generic collection of objects.

 Queue: It represents a first-in, first-out collection of objects.

 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.

Object Oriented Programming Fall-2023


Generic Collections Classes in C#
17

The Generic Collection Classes come with C# 2.0 and are defined under

the System.Collection.Generic namespace. It provides a generic implementation of standard data

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

stored in a generic collection, it eliminates accidental type mismatches. The System.Collections.Generic

namespace has the following classes:

 List<T>: It represents a strongly typed list of objects that can be accessed by index. Provides

methods to search, sort, and manipulate lists.

 Stack<T>: It represents a variable size last-in-first-out (LIFO) collection of instances of the same

specified type.

 Queue<T>: It represents a first-in, first-out collection of objects.

Object Oriented Programming Fall-2023


Generic Collections Classes in C#
18

 HashSet<T>: It represents a set of values. It removes duplicate elements from the collection.

 Dictionary<TKey, TValue>: It represents a collection of keys and values.

 SortedList<TKey, TValue>: It represents a collection of key/value pairs that are sorted by key

based on the associated System.Collections.Generic.IComparer implementation.

 SortedSet<T>: It represents a collection of objects that are maintained in sorted order.

 SortedDictionary<TKey, TValue>: It represents a collection of key/value pairs that are sorted

on the key.

 LinkedList<T>: It represents a doubly linked list.

Object Oriented Programming Fall-2023


Concurrent Collection Classes in C#
19

It came in .NET Framework Version 4 and onwards. It provides various threads-safe collection classes

that are used in place of the corresponding types in

the System.Collections and System.Collections.Generic namespaces, when multiple threads are

accessing the collection simultaneously. The System.Collections.Concurrent namespace provides

classes for thread-safe operations. Now multiple threads will not create problems for accessing the

collection items. The System.Collections.Concurrent namespace has the following classes:

 BlockingCollection<T>: It provides blocking and bounding capabilities for thread-safe collections that

implement System.Collections.Concurrent.IProducerConsumerCollection.

 ConcurrentBag<T>: It represents a thread-safe, unordered collection of objects.

 ConcurrentStack<T>: It represents a thread-safe last-in-first-out (LIFO) collection.

 ConcurrentQueue<T>: It represents a thread-safe first-in-first-out (FIFO) collection.

 ConcurrentDictionary<TKey, TValue>: It represents a thread-safe collection of key/value pairs that can be

accessed by multiple threads concurrently.

Object Oriented Programming Fall-2023


Non-Generic
Collections Classes

20
ArrayList Class
21

 It represents ordered collection of an object that can

be indexed individually.

 It is basically an alternative to an array. However, unlike

array you can add and remove items from a list at a


specified position using an index and the array resizes
itself automatically. It also allows dynamic memory
allocation, adding, searching and sorting items in the list.

Object Oriented Programming Fall-2023


Properties of ArrayList Class
22

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.

Object Oriented Programming Fall-2023


Methods of ArrayList Class
23

The following table lists some of the commonly used methods of


the ArrayList class
 public virtual int Add(object value);
Adds an object to the end of the ArrayList.
 public virtual void AddRange(ICollection c);
Adds the elements of an ICollection to the end of the ArrayList.
 public virtual void Clear();
Removes all elements from the ArrayList.
 public virtual bool Contains(object item);
Determines whether an element is in the ArrayList.

Object Oriented Programming Fall-2023


Methods of ArrayList Class
24

 public virtual ArrayList GetRange(int index, int count);

Returns an ArrayList which represents a subset of the elements in the source ArrayList.

 public virtual int IndexOf(object);

Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.

 public virtual void Insert(int index, object value);

Inserts an element into the ArrayList at the specified index.

 public virtual void InsertRange(int index, ICollection c);

Inserts the elements of a collection into the ArrayList at the specified index.

 public virtual void Remove(object obj);

Removes the first occurrence of a specific object from the ArrayList.

 public virtual void RemoveAt(int index);

Removes the element at the specified index of the ArrayList.

Object Oriented Programming Fall-2023


Methods of ArrayList Class
25

 public virtual void RemoveRange(int index, int count);

Removes a range of elements from the ArrayList.

 public virtual void Reverse();

Reverses the order of the elements in the ArrayList.

 public virtual void SetRange(int index, ICollection c);

Copies the elements of a collection over a range of elements in the ArrayList.

 public virtual void Sort();

Sorts the elements in the ArrayList.

 public virtual void TrimToSize();

Sets the capacity to the actual number of elements in the ArrayList.

Object Oriented Programming Fall-2023


Example
26

Object Oriented Programming Fall-2023


Hashtable Class
27

 The Hashtable class represents a collection of key-and-


value pairs that are organized based on the hash code
of the key. It uses the key to access the elements in the
collection.
 A hash table is used when you need to access elements by
using key, and you can identify a useful key value. Each
item in the hash table has a key/value pair. The key is
used to access the items in the collection.

Object Oriented Programming Fall-2023


Properties of the Hashtable Class
28

The following table lists some of the commonly used properties of the Hashtable class

 Count

Gets the number of key-and-value pairs contained in the Hashtable.

 IsFixedSize

Gets a value indicating whether the Hashtable has a fixed size.

 IsReadOnly

Gets a value indicating whether the Hashtable is read-only.

 Item

Gets or sets the value associated with the specified key.

 Keys

Gets an ICollection containing the keys in the Hashtable.

 Values

Gets an ICollection containing the values in the Hashtable.


Object Oriented Programming Fall-2023
Methods of the Hashtable Class
29

The following table lists some of the commonly used methods of the Hashtable class

 public virtual void Add(object key, object value);

Adds an element with the specified key and value into the Hashtable.

 public virtual void Clear();

Removes all elements from the Hashtable.

 public virtual bool ContainsKey(object key);

Determines whether the Hashtable contains a specific key.

 public virtual bool ContainsValue(object value);

Determines whether the Hashtable contains a specific value.

 public virtual void Remove(object key);

Removes the element with the specified key from the Hashtable.

Object Oriented Programming Fall-2023


Example -- Hashtable Class
30

Object Oriented Programming Fall-2023


SortedList Class
31

 The SortedList class represents a collection of key-and-value


pairs that are sorted by the keys and are accessible by key and
by index.
 A sorted list is a combination of an array and a hash table. It
contains a list of items that can be accessed using a key or an
index. If you access items using an index, it is an ArrayList,
and if you access items using a key, it is a Hashtable. The
collection of items is always sorted by the key value.

Object Oriented Programming Fall-2023


Properties of the SortedList Class
32

The following table lists some of the commonly used properties of the SortedList
class −

 Capacity - Gets or sets the capacity of the SortedList.

 Count - Gets the number of elements contained in the SortedList.

 IsFixedSize - Gets a value indicating whether the SortedList has a fixed size.

 IsReadOnly - Gets a value indicating whether the SortedList is read-only.

 Item - Gets and sets the value associated with a specific key in the SortedList.

 Keys - Gets the keys in the SortedList.

 Values - Gets the values in the SortedList.

Object Oriented Programming Fall-2023


Methods of the SortedList Class
33

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

value into the SortedList.

 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 IList GetKeyList(); -- Gets the keys in the SortedList.

Object Oriented Programming Fall-2023


Methods of the SortedList Class
34

 public virtual IList GetValueList(); -- Gets the values in 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.

Object Oriented Programming Fall-2023


Example -- SortedList Class
35

Object Oriented Programming Fall-2023


Stack Class
36

It represents a last-in, first out collection of object. It is used

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

remove it, it is called popping the item.

Object Oriented Programming Fall-2023


Properties of the Stack Class
37

The following table lists some commonly used properties of

the Stack class


 Count - Gets the number of elements contained in the Stack.

Object Oriented Programming Fall-2023


Methods of the Stack Class
38

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 bool Contains(object obj); -- Determines whether an element is in 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.

 public virtual object[] ToArray(); -- Copies the Stack to a new array.

Object Oriented Programming Fall-2023


Example -- Stack Class
39

Object Oriented Programming Fall-2023


Queue Class
40

It represents a first-in, first out (FIFO) collection of object. It is

used when you need a first-in, first-out access of items. When

you add an item in the list, it is called enqueue, and when you

remove an item, it is called deque.

Object Oriented Programming Fall-2023


Properties of the Queue Class
41

The following table lists some of the commonly


used properties of the Queue class
 Count -- Gets the number of elements contained in the
Queue.

Object Oriented Programming Fall-2023


Methods of the Queue Class
42

The following table lists some of the commonly used methods of


the Queue class
 public virtual void Clear(); -- Removes all elements from the Queue.

 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 object[] ToArray(); -- Copies the Queue to a new array.

 public virtual void TrimToSize(); -- Sets the capacity to the actual number of elements in the
Queue.

Object Oriented Programming Fall-2023


Example -- Queue Class
43

Object Oriented Programming Fall-2023


You have the quality of education, life give
you more opportunity for new area of
learning to achieve your dreams.

THANK YOU.

44

You might also like