Table Of ContentBeginning C# Objects:
From Concepts to Code
JACQUIE BARKER AND GRANT PALMER
APress Media, LLC
Beginning C# Objects: From Concepts to Code
Copyright ©2004 by Jacquie Barker and Grant Palmer
Originally published by Apress in 2004
AII rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information storage
or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN 978-1-59059-360-8 ISBN 978-1-4302-0691-0 (eBook)
DOI 10.1007/978-1-4302-0691-0
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Lead Editor: Dominic Shakeshaft
Technical Reviewer: James Huddleston
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis,
John Franklin, Jason Gilmore, Chris Mills, Steve Rycroft, Dominic Shakeshaft, Jim Sumser, Karen
Watterson, Gavin Wray, John Zukowski
Project Manager: Kylie Johnston
Copy Edit Manager: Nicole LeClerc
Copy Editor: Ami Knox
Production Manager: Kari Brooks
Production Editor: Laura Cheu
Compositor: Kinetic Publishing Services, LLC
Proofreader: Liz Welch
Indexer: Michael Brinkman
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
The information in this book is distributed on an "as is" hasis, without warranty. Although every
precaution has been taken in the preparation ofthis work, neither the author(s) nor Apress shall
hav e any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused direcdy or indirecdy by the information contained in this work.
In loving memory of my wonderful parents, Bill and Dorothy lost.
-Jacquie Barker
I would like to dedicate this book to my dog Bailey, who is a good, old dog.
-Grant Palmer
Contents at a Glance
About the Authors ................................................ xiii
Acknowledgments .................................................... xv
Preface ........................................................... xvii
Introduction ...................................................... xix
Part One: The ABCs of Objects ............................ .
1
Chapter 1 A little Taste of C# ................................ 3
Chapter 2 Abstraction and Modeling .......................... 55
Chapter 3 Objects and Classes ................................ 69
Chapter 4 Object Interactions ................................ 91
Chapter 5 Relationships Between Objects .................... 159
Chapter 6 Collections of Objects ............................ 201
Chapter 7 Some Final Object Concepts ....................... 237
Part Two: Object Modeling 101 ........................... 293
Chapter 8 The Object Modeling Process in a Nutshell ...... 295
Chapter 9 Formalizing Requirements Through Use Cases ..... 307
Chapter 10 Modeling the Static/Data Aspects of the
System .............................................. 321
Chapter 11 Modeling the Dynamic/Behavioral Aspects of
the System ......................................... 383
Chapter 12 Wrapping Up Our Modeling Efforts ................ 409
Part Three:Translating a UML "Blueprint" into C#
Code ...............................................
419
Chapter 13 A Deeper Look at C# ............................... 421
Chapter 14 Transforming Our UML Model into C# Code ........ 547
Chapter 15 Rounding Out Our Application, Part 1:
Adding File Persistence .......................... 611
v
Contents at a Glance
Chapter 16 Rounding Out Our Application, Part 2:
Adding a Graphical User Interface ...............
667
Chapter 17 Next Steps .........................................
763
Appendix A Suggestions for Using This Book As
a Textbook .........................................
769
Appendix B Alternative Case Studies .........................
775
Appendix C Setting Up a Basic Object Modeling/C#
Environment ........................................
783
Appendix D Downloading and Compiling the SRS
Source Code ........................................
791
Appendix E C# Keywords ........................................
793
Index ............................................................. .
799
vi
Contents
About the Authors ................................................ xiii
Acknowledgments .................................................... xv
Preface ........................................................... xvii
Introduction ...................................................... xix
Part One: The ABCs of Objects
Chapter 1 A Little Taste of C# .......................... 3
Getting "Hands On" with C# ........................................ 4
Why C#? ............................................................. 4
C# Language Basics ................................................. 7
Anatomy of a Simple C# Program ................................... B
Predefined Types .................................................. 11
Variables .......................................................... 12
Strings ............................................................ . 15
Case Sensitivity .................................................. 16
C# Expressions ..................................................... 16
Implicit Type Conversions and Explicit Casting ................ 22
Loops and Other Flow of Control Structures .................... 24
Jump Statements .......................... 36
0. 0 0 ••• 0 0 0 0 0 0 •• 0 •••••••••
Code Blocks and Variable Scope .................................. 37
Printing to the Screen .... 39
0 ••••••••••••••••• 0 ••••••••••••••••••••
Elements of C# Style .............................................. 43
Summary 51
0 •••• 0 •••••• 0 •• 0 •••••••••••••••••••••••••••••••••••••••••••••
Exercises .......................................................... 52
Chapter 2 Abstraction and Modeling .................... 55
Simplification Through Abstraction .............................. 55
Generalization Through Abstraction .............................. 56
Reuse of Abstractions ........................... 62
0 •••••••••••••••••
Inherent Challenges ....................................... 63
0 •••••••
Summary ............................... 66
0 •••••••••••••••••••••••••••••
Exercises .......................................................... 66
Chapter 3 Objects and Classes ...........................
69
What Is an Object? ................................................ 69
State/A ttributes/Data ............................................. 70 vii
Contents
Behavior /Operations/Methods ...................................... 72
Classes ............................................................. 74
User-Defined Types and Reference Variables ..................... 79
Instantiating Objects: A Closer Look ........................... 80
Objects As Attributes ............................................. 84
Composition ........................................................ 86
Three Distinguishing Features of an Object-Oriented
Programming Language ........................................... 89
Summary ............................................................. 89
Exercises .......................................................... 90
Chapter 4 Object Interactions ...........................
91
Events Drive Object Collaboration ............................... 91
Declaring Methods ................................................. 93
Method Invocation and Dot Notation ............................. 102
Information Hiding/Accessibility ............................... 118
The Power of Encapsulation ...................................... 142
Instance Constructors ............................................ 151
Summary ............................................................ 155
Exercises ......................................................... 156
Chapter 5 Relationships Between Objects ............
159
Associations and Links .......................................... 160
Inheritance ....................................................... 168
Three Distinguishing Features of an Object-Oriented
Programming Language, Revisited .............................. 196
Summary ............................................................ 197
Exercises ......................................................... 197
Chapter 6 Collections of Objects ...................... 201
What Are Collections? ........................................... 201
Arrays As Simple Collections ................................... 205
More Sophisticated Collection Types ............................ 215
Referencing the Same Object Simultaneously from
Multiple Collections .......................................... 220
Inventing Our Own Collection Types ............................. 221
Collections As Method Return Types ............................. 227
Collections of Supertypes ....................................... 228
Composite Classes, Revisited .................................... 230
Summary ............................................................ 234
Exercises ......................................................... 235
viii
Contents
Chapter 7 Some Final Object Concepts ................
237
What Is Polymorphism? ........................................... 238
Three Distinguishing Features of an Object-Oriented
Programming Language .......................................... 247
Abstract Classes ................................................. 248
Interfaces ........................................................ 256
Static Features .................................................. 271
Utility Classes .................................................. 283
Summary ............................................................ 288
Exercises ......................................................... 289
Part Two: Object Modeling 101
Chapter 8 The Object Modeling Process in
a Nutshell ......................................
295
The "Big Picture" Goal of Object Modeling .................... 295
Our Object Modeling Process, in a Nutshell ................... 300
Summary ............................................................ 304
Exercises ......................................................... 305
Chapter 9 Formalizing Requirements Through
Use Cases .......................................
307
What Are Use Cases? .............................................. 308
Actors ............................................................. 310
Spedf ying Use Cases ............................................. 315
Matching Up Use Cases with Actors .............................. 316
To Diagram or Not to Diagram? .................................. 317
Summary ............................................................ 319
Exercises ......................................................... 319
Chapter 10 Modeling the Static/Data Aspects
of the System ..................................
321
Identifying Appropriate Classes ................................ 322
Producing a Data Dictionary ..................................... 336
Determining Associations Between Classes ...................... 338
Identifying Attributes .......................................... 343
UML Notation: Modeling the Static Aspects
of an Abstraction .............................................. 343
Object Diagrams .................................................. 357
ix
Contents
Information "Flows" Along the Association "Pipeline" ........ 361
"Mixing and Matching" Relationship Notations ................. 366
Association Classes .............................................. 369
Our Completed Student Registration System
Class Diagram .................................................. 372
Metadata .......................................................... 379
Summary ............................................................ 381
Exercises ......................................................... 382
Chapter 11 Modeling the Dynamic/Behavioral
Aspects of the System .......................
383
How Behavior Affects State ...................................... 384
Scenarios ......................................................... 391
Sequence Diagrams ................................................ 395
Using Sequence Diagrams to Determine Methods ................. 401
Collaboration Diagrams .......................................... 404
Revised SRS Class Diagram ....................................... 405
Summary ............................................................ 407
Exercises ......................................................... 407
Chapter 12 Wrapping Up Our Modeling Efforts ........ 409
Testing Your Model ............................................... 409
Revisiting Requirements ......................................... 410
Reusing Models: A Word About Design Patterns ................. 413
Summary ............................................................ 416
Exercises ......................................................... 417
Part Three:Translating a UML "Blueprint" into C#
Code
Chapter 13 A Deeper Look at C# .........................
421
Sorting Out Terminology ......................................... 422
Setting Up a C# Programming Environment ....................... 424
Compiling and Running C# Programs .............................. 424
Namespaces ........................................................ 438
Strings As Objects ............................................... 445
C# Exception Handling ........................................... 452
The Object Class ................................................. 466
C#' s Collection Classes ......................................... 474
Initialization of Variables, Revisited ........................ 498
More About the Main Method ...................................... 500
X