0% found this document useful (0 votes)
7 views30 pages

SE OOP Lecture15

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)
7 views30 pages

SE OOP Lecture15

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/ 30

OBJECT ORIENTED PROGRAMMING

BS – (SOFTWARE ENGINEERING)

FALL SEMESTER - 2023

LECTURE # 15

COURSE INSTRUCTOR: AMJAD ALI KHAWAJA

1
Contents
2

 Problems with Non-Generic Collections in C#

 The Solution to Non-Generic Collection Problems

 What are Generic Collections in C#?

 Namespace of Generic Collection

 List<T> Class

 HashSet<T> Class

 SortedSet<T> Class

 Stack<T> Class

 Queue<T> Class

 Dictionary<TKey, TValue> Class

 SortedDictionary<TKey, TValue> Class

 SortedList<TKey, TValue> Class

Object Oriented Programming Fall-2023


Any Queries From
Previous Lectures

3
Problems with Non-Generic Collections in C#
4

The Non-Generic Collection Classes such as ArrayList, Hashtable, SortedList, Stack,


and Queue are worked on the object data type. That means the elements added to the
collection are of an object type. As these Non-Generic CollectionClasses worked on object
data type, we can store any type of value that may lead to runtime exceptions due to type
mismatch. But with Generic Collections, now we are able to store a specific type of data
(whether a primitive type or a reference type) which provides type safety by eliminating the
type mismatch at run time.

Object Oriented Programming Fall-2023


Example -- Problems with Non-Generic Collections in C#
5

Object Oriented Programming Fall-2023


Problems with Non-Generic Collections in C#
6

The Second Problem with Non-Generic Collection Classes is that we get performance
overhead. The reason for this is Boxing and Unboxing. As we already discussed these
collection classes worked on the object data type. So if we are storing value-type data in the
collection, then those value-type data are first converted into object type and then only store
into the collection which is nothing but performing Boxing. Similarly, if we want to retrieve
the data from the collection, then we need to convert the data from object type to value type
means performing Unboxing. Due to this Boxing and Unboxing, we get poor performance if
our collection is a big one.

Object Oriented Programming Fall-2023


Example -- Problems with Non-Generic Collections in C#
7

Object Oriented Programming Fall-2023


Point TO Remember
8

Note: Boxing means converting a value type to an object type and Unboxing means
converting an object type back to the value type.

Object Oriented Programming Fall-2023


The Solution to Non-Generic Collection Problems
9

 The above two problems of Non-Generic Collections are overcome by using the Generic

Collections in C#. The .NET Framework has re-implemented all the existing Non-Generic
Collection classes such as ArrayList, Hashtable, SortedList, Stack, and Queue., etc.
in Generic Collections such as ArrayList<T>, Dictionary<TKey, TValue>,
SortedList<TKey, TValue>, Stack<T>, and Queue<T>. Here T is nothing but the
type of values that we want to store in the collection. So, the most important point that
you need to remember is while creating the objects of Generic Collection Classes, you
need to explicitly provide the type of values that you are going to store in the collection.

 A Generic Collection is Strongly Type-Safe. Which type of data do you want to store in

generic type, this information you have to provide at compile time. It means you can only
put one type of data into it. This eliminates type mismatches at runtime.

Object Oriented Programming Fall-2023


Example -- The Solution to Non-Generic Collection Problems
10

It is also possible to store a user-defined type like a class type or structure type as shown below

List<Customer> customerList = new List<Customer> ();

Assume the Customer is a user-defined class type that represents an entity Customer, Here we can store
the customer objects within the customerList collection where each customer object can internally
represent different attributes of the customer like Id, Name, City, State, etc.

Object Oriented Programming Fall-2023


What are Generic Collections in C#?
11

Generic Collections are introduced as part of C# 2.0. You can consider this Generic Collection as an
extension to the Non-Generic Collection Classes which we have already discussed in our previous lecture
such as ArrayList, Hashtable, SortedList, Stack, and Queue.

The Generic Collections in C# are strongly typed. The strongly typed nature allows these collection classes
to store only one type of value into it. This not only eliminates the type mismatch at runtime but also will
get better performance as they don’t require boxing and unboxing while they store value type data. So, it is
always a preferable and a good programming choice to use the Generics Collection Classes in C# rather
than using the Non-Generic Collection Classes.

Note: In most cases, it is recommended to use the generic collections because they perform faster than
non-generic collections and also minimize exceptions by giving compile-time errors.

Object Oriented Programming Fall-2023


Namespace of Generic Collection
12

The Generic Collection Classes are implemented under the System.Collections.Generic namespace.
The classes which are present in this namespace are as follows.
 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.

 HashSet<T>: It represents a set of values. It eliminates duplicate elements.

 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. It automatically adds the elements in ascending order of key by
default.

 List<T>: It represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and
manipulate lists. It grows automatically as you add elements to it.

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

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

Note: Here the <T> refers to the type of values we want to store under them.

Object Oriented Programming Fall-2023


List<T> Class
13

The List<T> Generic Collection Class in C# is used to store and fetch elements. It can have
duplicate elements. It belongs to System.Collections.Generic namespace. You can also
consider the List<T> collection as the generic version of ArrayList. Here, we need to
mention the type of values that we want to store in the collection. Like, ArrayList we are
unable to add any types of value into the List<T> collection, which prevents us from runtime
exceptions due to type mismatch.

Object Oriented Programming Fall-2023


Example -- List<T> Class in C#
14

Object Oriented Programming Fall-2023


HashSet<T> Class
15

The Generic HashSet<T> Collection Class in C# can be used to store, remove or view
elements. It does not allow the addition of duplicate elements. It is suggested to use the
HashSet class if you have to store only unique elements. It belongs to
System.Collections.Generic namespace.

Object Oriented Programming Fall-2023


Example -- HashSet<T> Class in C#
16

Object Oriented Programming Fall-2023


SortedSet<T> Class
17

The Generic SortedSet<T> Collection Class in C# is used to store, remove or view elements.
By default, it stores the elements in ascending order and does not store duplicate elements.
It is recommended to use if you have to store unique elements as well as if you want to
maintain ascending order. The SortedSet<T> class belongs to System.Collections.Generic
namespace. Let’s see an example of a generic SortedSet<T> collection class in C# that stores
elements using Add() method and iterates elements using for-each loop.

Object Oriented Programming Fall-2023


Example -- SortedSet<T> Class in C#:
18

Object Oriented Programming Fall-2023


Stack<T> Class
19

The Generic Stack<T> Collection Class in C# is used to push and pop elements in LIFO (Last
in First Out) order. The push operation adds an element to a collection, whereas the pop
operation is used to remove the most recently added element from a collection. It can have
duplicate elements. The Stack<T> class belongs to System.Collections.Generic namespace.
Let’s see an example of a generic Stack<T> collection class in C# that stores elements using
the Push() method, removes elements using the Pop() method and iterates elements using
for-each loop.

Object Oriented Programming Fall-2023


Example -- Stack<T> Class in C#
20

Object Oriented Programming Fall-2023


Queue<T> Class
21

The Generic Queue<T> Collection Class in C# is used to Enqueue and Dequeue elements in
FIFO (First In First Out) order. The Enqueue operation adds an element in a collection,
whereas the Dequeue operation is used to remove the firstly added element from the queue
collection. It can have duplicate elements. The Queue<T> Collection Class belongs to
System.Collections.Generic namespace. Let’s see an example of a generic Queue<T>
collection class in C# that add elements using Enqueue() method, removes elements using
Dequeue() method and iterates elements using the for-each loop.

Object Oriented Programming Fall-2023


Example -- Queue<T> Class in C#:
22

Object Oriented Programming Fall-2023


Dictionary<TKey, TValue> Class
23

The Generic Dictionary<TKey, TValue> Collection Class in C# is the generic version of


Hashtable. It works as same as the Hashtable except that it operates on a type object, and
this is one of the most useful collections based on key and value pairs. It stores values on the
basis of keys. It contains unique keys only. With the help of the key, we can easily search or
remove elements. The Dictionary<TKey, TValue> Collection Class belongs to
System.Collections.Generic namespace. Let’s see an example of a generic Dictionary<TKey,
TValue> collection class in C# that stores elements using Add() method and iterates
elements using for-each loop. Here, we are using KeyValuePair class to get keys and values.

Object Oriented Programming Fall-2023


Example -- Dictionary<TKey, TValue> Class in C#:
24

Object Oriented Programming Fall-2023


SortedDictionary<TKey, TValue> Class
25

The Generic SortedDictionary<TKey, TValue> Collection Class in C# works similar to


Dictionary<TKey, TValue> collection class. It stores values on the basis of keys. It contains
unique keys and the most important thing is it stored the elements in ascending order on the
key. With the help of a key, we can easily search or remove elements. The
SortedDictionary<TKey, TValue> Collection Class belongs to System.Collections.Generic
namespace. Let’s see an example of a generic SortedDictionary<TKey, TValue> collection
class in C# that stores elements using Add() method and iterates elements using the for-each
loop. Here, we are using KeyValuePair class to get keys and values.

Object Oriented Programming Fall-2023


Example -- SortedDictionary<TKey, TValue> Class in C#
26

Object Oriented Programming Fall-2023


SortedList<TKey, TValue> Class
27

 The Generic SortedList<TKey, TValue> Collection Class in C# is a collection of key/value

pairs that are sorted according to keys. By default, this collection sort the key/value pairs
in ascending order. With the help of a key, we can easily search, or remove elements. The
SortedList<TKey, TValue> class belongs to System.Collections.Generic namespace.

 Let’s see an example of a generic SortedList<TKey, TValue> collection class in C# that

stores elements using Add() method and iterates elements using the for-each loop. Here,
we are using KeyValuePair class to get keys and values.

Object Oriented Programming Fall-2023


Example -- SortedList<TKey, TValue> Class in C#
28

Object Oriented Programming Fall-2023


Point to remember
29

Generic Collections in C#.


 Array: Type-Safe but Fixed Length

 Collections: Auto Resizing but not Type-Safe

 Generic Collections: Type-Safe and Auto-Resizing

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.

30

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy