Object Oriented Programming using C#

(backadmin) #1

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


Like lists, sets are resized automatically as items are added


Many of the operations available for a List are also available for a Set, but
• we CANNOT add an object at a specific position since the elements are not in any order
• we CANNOT ‘replace’ an item for the same reason (though we can add one and delete another)
• retrieving all the items is possible but the sequence is indeterminate
• it is meaningless to find what position an element is in.


7.6 Dictionaries


Dictionaries are rather different from Lists and Sets because instead of storing individual objects they store pairs of
objects. The pair is made up of a ‘key’ and a ‘value’. The key is something which identifies the pair. The value is a piece of
information or an object associated with the key.


In language dictionaries that we are all familiar with the ‘key’ is the word you look up and the ‘value’ the meaning of that
word. Of course in dictionaries that we are familiar with the entries are sorted into work (or key) order. If that was not
the case it would be very difficult to find a word in a dictionary.


Another example would be an address book: in an address book the keys would be people’s names and the values their
address, phone, email etc. There is only one value for each key, but since values are objects they can contain several pieces
of data.


Duplicate keys are not permitted, though duplicate values are. So in the previous example if you looked up two people in
the address book you may find them living at the same address – but one person would not have two homes.


Like a set, a Dictionary does not arrange items in order (but a SortedDictionary does, in order of keys) and like lists and
sets, dictionaries are resized automatically as items are added or deleted.

Free download pdf