Object Oriented Programming using C#

(backadmin) #1

Object Oriented Programming using C#
Generic Collections and how to Serialize them


To make accounts truly unique we could amend the definition of Account to contain an account number that we will
ensure is unique and we could continue to store these using a set. However as we will often retrieve account using an
account number a dictionary would be a more efficient collection for this application.


A dictionary is like a set but where each item stored is made up of a key/value pair.


In this case the key would be a unique account number and the value would be the associated bank account object.


In the previous example we stored a collection of Accounts in a set, we do not need to make any changes to the Account
class if we wish to store these in a dictionary.


We could remove the overridden methods for Equals() and GetHashCode() from the Account class as a dictionary can
contain duplicate values. Alternatively we could leave these methods in should we wish to.


In a dictionary it is the keys that are unique.

Free download pdf