session 07 module 13 - collections. collections / session 7 / 2 of 32 review a delegate in c# is...

32
Session 07 Module 13 - Collections

Upload: malcolm-gray

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Session 07

Module 13 - Collections

Page 2: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 2 of 32

Review

A delegate in C# is used to refer to a method in a safe manner.

To invoke the mothod that is referred to by the delegate, an instance of the delegate is created.

A Event is a data member that enables an object to provide notifications to other objects about a particular action.

For using an event, you must first declare a delegate and an event, define an event handler to handle the event and finally associate the handler to the event.

Page 3: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 3 of 32

Module 13 – Objectives (1)

List and explain the commonly used classes and interfaces in the System.Collections namespace

List and explain the commonly used classes and interface in the System.Collection.Generic namespace

Describle the commonly used methods and properties of ArrayList class

Page 4: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 4 of 32

Module 13 – Objectives (2)

Describe the commonly used methods and properties of Hashtable class

Describe the commonly used methods and properties of SortedList class

Describe the commonly used methods and properties of Dictionary class

Page 5: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 5 of 32

What is Collection?

A collection is a set of related data that may not necessary belong to the same data type.

Element in collection can be set or modified at run time

Page 6: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 6 of 32

System.Collections namespace

System.Collections namespace in C# allow to construct and manipulate a collection of objects.

This collection can include elements of difference data types.

Page 7: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 7 of 32

Classes

Page 8: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 8 of 32

Interface

Page 9: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 9 of 32

System.Collections.Generic namespace

Generic allow you to define data structures that consist of functionalities which can be implemented for any data type.

Generic allow you to reuse a code for different data types.

System.Collections.Generic namespace is similar to the System.Collections namespace as both allow you to create collections. However, generic collection are type safe

Page 10: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 10 of 32

System.Collections.Generic class

Page 11: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 11 of 32

System.Collections.Generic Interface

Page 12: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 12 of 32

ArrayList class ArrayList class is a variable-length array that can dynamically

increase or decrease in size.

Unlike Array class, this class can store elements of different data type.

The default capacity of an ArrayList is 16, if the number of elements in list reached the specified capacity, the capacity of list is doubled automatically.

It can accept null value and can also include duplicate elements

Page 13: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 13 of 32

Methods

Page 14: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 14 of 32

Properties

Page 15: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 15 of 32

Hash table class

Hashtable class allow you to create collections in the form of keys and values.

It generates a hash table which associated keys with their corresponding values.

Page 16: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 16 of 32

Methods

Page 17: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 17 of 32

Property

Page 18: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 18 of 32

SortedList class

SortedList class presents a collections of pair value and Key

By default SortedList sorts the elements in ascending order.

These elements either accessed using the corresponding keys or index number

Page 19: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 19 of 32

Hashtable vs. SortedList

SortedList class is a combination of the Hashtable class and the ArrayList class.

The difference between the Hashtable class and the SortedList class is that the values in object of SortedList class are sorted by their keys as well as by their index position.

However, the execution of the program using the SortedList class is slower than the programming using the Hashtable class due to sorting process.

Page 20: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 20 of 32

Hashtable vs. SortedList

Page 21: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 21 of 32

Methods

Page 22: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 22 of 32

Property

Page 23: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 23 of 32

Dictionary generic class

It consists of a generic collection of elements organized in key and value pairs.

It maps the keys to their corresponding values.

Unlike other collections in the System.Collections namespace, it is used to create a collection of a single data type at a time

The Dictionary class does not allow null value as elements.

Page 24: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 24 of 32

Methods

Page 25: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 25 of 32

Properties

Page 26: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 26 of 32

Dictionary Generic class System.Collection.Generic namespace contains

commonly used class is Dictionary generic class.

Dictionary generic class consists of a generic collection of elements organized in key and value pair.

Page 27: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 27 of 32

Dictionary generic class

Unlike other collection in the System.Collections namespace, it is used to create a collection of a single data type at a time.

Dictionary does not allow null value as elements.

Dictionary<Tkey, TValue>

Page 28: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 28 of 32

Method

Page 29: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 29 of 32

Property

Page 30: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 30 of 32

Session 13 – Summary (1)

Collection allow you to manage a group of objects dynamically at run-time.

The System.Collections namespace contains different collections such as arrays, hash table, list and dictionary.

System.Collection.Generic consists of generic collections that allow resuability of code and provide better type safety.

Page 31: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 31 of 32

Session 13 – Summary (2)

ArrayList class allow to increase and decrease the size of the collection during program execution.

It allow you to store elements of different data types.

Hashtable class stores elements as key and value pair where the data is organized based on the hash code.

Each value in the hash table is uniquely identified by its key

Page 32: Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke

Collections / Session 7 / 32 of 32

Session 13 – Summary (3)

SortedList class allow you to store elements as key and value pairs where the data is sorted based on the key.

It allow you to access elements from the collection using the key or the index number.

Dictionary generic class represents a collection of elements organized in key and value pairs.

The key value is used to retrieve value from the collection