Object Oriented Programming using C#

(backadmin) #1

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


The results of running this program are shown below.


7.12 Summary


The .NET Collections classes provide ready-made methods for storing collections of objects. These almost completely
make the use of arrays redundant!


There are ‘untyped’ collections and ‘typed’ collections. Typed collections, or generic collections, were developed based
upon the idea of generic methods and generic classes. The use of generic collections are much more useful than untyped
collections and thus we have not shown the use untyped collections here.


Collection classes include List, HashSet and Dictionary. Each of these define appropriate methods, many of which are
common across all of the collection classes.


Special attention is required when defining objects to be stored in Sets (or as keys in Dictionaries) to define the meaning
of ‘duplicate’. For these we need to override the Equals() and GetHashCode() methods inherited from Object.


Serialization is a very powerful mechanism that allows the entire contents of a collection and all related objects to be
stored with one simple command.


A further example of the use of collections will be provided in Chapter 11, a larger case study at the end of this book.
This will demonstrate the use of lists, sets and dictionaries for a small but more realistic example application. The code
for this will be available to download and inspect if required.

Free download pdf