Deitel® Series Page
How To Program Series
Android How to Program
C How to Program, 7/E
C++ How to Program, 9/E
C++ How to Program, Late Objects Version, 7/E
Java™ How to Program, 9/E
Java™ How to Program, Late Objects Version, 8/E
Internet & World Wide Web How to Program, 5/E
Visual Basic® 2012 How to Program
Visual C#® 2012 How to Program, 5/E
Visual C++® 2008 How to Program, 2/E
Small Java™ How to Program, 6/E
Small C++ How to Program, 5/E
Simply Series
Simply C++: An App-Driven Tutorial Approach
Simply Java™ Programming: An App-Driven
Tutorial Approach
Simply Visual Basic® 2010, 4/E: An App-Driven
Tutorial Approach
CourseSmart Web Books
www.deitel.com/books/CourseSmart/
C++ How to Program, 7/E, 8/E & 9/E
Simply C++: An App-Driven Tutorial Approach
Java™ How to Program, 7/E, 8/E & 9/E
Simply Visual Basic 2010: An App-Driven
Approach, 4/E
Visual Basic® 2012 How to Program
Visual Basic® 2010 How to Program
Visual C#® 2012 How to Program, 5/E
Visual C#® 2010 How to Program, 4/E
Deitel® Developer Series
C++ for Programmers, 2/E
Android for Programmers: An App-Driven
Approach
C# 2010 for Programmers, 3/E
Dive Into® iOS 6: An App-Driven Approach
iOS 6 for Programmers: An App-Driven Approach
Java™ for Programmers, 2/E
JavaScript for Programmers
LiveLessons Video Learning Products
www.deitel.com/books/LiveLessons/
Android® App Development Fundamentals
C++ Fundamentals
C# Fundamentals
iOS 6 App Development Fundamentals
Java™ Fundamentals
JavaScript Fundamentals
Visual Basic® Fundamentals
To receive updates on Deitel publications, Resource Centers, training courses, partner offers and more,
please register for the free Deitel ® Buzz Online e-mail newsletter at:
www.deitel.com/newsletter/subscribe.html
and join the Deitel communities on Twitter®
@deitel
Facebook®
facebook.com/DeitelFan
and Google+
gplus.to/deitel
To communicate with the authors, send e-mail to:
[email protected]
For information on government and corporate Dive-Into® Series on-site seminars offered by Deitel &
Associates, Inc. worldwide, visit:
www.deitel.com/training/
or write to
[email protected]
For continuing updates on Prentice Hall/Deitel publications visit:
www.deitel.com
www.pearsonhighered.com/deitel/
Visit the Deitel Resource Centers that will help you master programming languages, software development, Android and iPhone/iPad app development, and Internet- and web-related topics:
www.deitel.com/ResourceCenters.html
Paul Deitel
Deitel & Associates, Inc.
Harvey Deitel
Deitel & Associates, Inc.
Vice President and Editorial Director: Marcia J. Horton
Executive Editor: Tracy Johnson
Associate Editor: Carole Snyder
Director of Marketing: Christy Lesko
Marketing Manager: Yezan Alayan
Marketing Assistant: Jon Bryant
Director of Production: Erin Gregg
Managing Editor: Scott Disanno
Associate Managing Editor: Robert Engelhardt
Operations Specialist: Lisa McDowell
Art Director: Anthony Gemmellaro
Cover Design: Abbey S. Deitel, Harvey M. Deitel, Anthony Gemmellaro
Cover Photo Credit: © Shutterstock/Excellent backgrounds
Media Project Manager: Renata Butera
Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear
on page vi.
The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the
development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation
contained in this book. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.
Copyright © 2014, 2011, 2009 Pearson Education, Inc., publishing as Prentice Hall. All rights reserved. Manufactured in the United States of America. This publication is protected by Copyright, and permission should be obtained
from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or
by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission(s) to use material
from this work, please submit a written request to Pearson Education, Inc., One Lake Street, Upper Saddle River, New
Jersey 07458, or you may fax your request to 201-236-3290.
Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where
those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been
printed in initial caps or all caps.
Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in
the documents and related graphics published as part of the services for any purpose. All such documents and related
graphics are provided “as is” without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim
all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and non-infringement. In no event
shall Microsoft and/or its respective suppliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious
action, arising out of or in connection with the use or performance of information available from the services.
The documents and related graphics contained herein could include technical inaccuracies or typographical errors.
Changes are periodically added to the information herein. Microsoft and/or its respective suppliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time. Partial screen shots may
be viewed in full within the software version specified.
Library of Congress Cataloging-in-Publication Data on file.
10 9 8 7 6 5 4 3 2 1
ISBN-10: 0-13-337933-7
ISBN-13: 978-0-13-337933-4
To our review team
Shay Friedman
Octavio Hernandez
Stephen Hustedde
José Antonio González Seco
Shawn Weisfeld
We are grateful for your guidance and expertise.
Paul and Harvey Deitel
Trademarks
DEITEL, the double-thumbs-up bug and DIVE INTO are registered trademarks of Deitel and Associates,
Inc.
Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A. and
other countries. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation.
UNIX is a registered trademark of The Open Group.
Throughout this book, trademarks are used. Rather than put a trademark symbol in every occurrence of
a trademarked name, we state that we are using the names in an editorial fashion only and to the benefit
of the trademark owner, with no intention of infringement of the trademark.
Contents
Chapters 24–35 and Appendices D–G are PDF documents posted online at the book’s
Companion Website (located at www.pearsonhighered.com/deitel/).
Preface
Before You Begin
1
Introduction to Computers, the Internet
and Visual C#
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
Introduction
Hardware and Moore’s Law
Data Hierarchy
Computer Organization
Machine Languages, Assembly Languages and High-Level Languages
Object Technology
Internet and World Wide Web
C#
1.8.1 Object-Oriented Programming
1.8.2 Event-Driven Programming
1.8.3 Visual Programming
1.8.4 An International Standard; Other C# Implementations
1.8.5 Internet and Web Programming
1.8.6 Introducing async/await
1.8.7 Other Key Contemporary Programming Languages
Microsoft’s .NET
1.9.1 .NET Framework
1.9.2 Common Language Runtime
1.9.3 Platform Independence
1.9.4 Language Interoperability
Microsoft’s Windows® Operating System
Windows Phone 8 for Smartphones
1.11.1 Selling Your Apps in the Windows Phone Marketplace
1.11.2 Free vs. Paid Apps
1.11.3 Testing Your Windows Phone Apps
Windows Azure™ and Cloud Computing
Visual Studio Express 2012 Integrated Development Environment
1.9
1.10
1.11
1.12
1.13
xix
xxxv
1
2
2
3
6
7
8
10
12
12
12
12
12
13
13
13
14
14
15
15
15
16
17
18
18
18
19
19
viii
Contents
Test-Drive in Visual Studio Express 2012 for Windows Desktop
Test-Drive in Visual Studio Express 2012 for Windows 8
1.14
1.15
Painter
Painter
2
Dive Into® Visual Studio Express 2012
for Windows Desktop
2.1
2.2
2.3
2.4
2.7
2.8
Introduction
Overview of the Visual Studio Express 2012 IDE
Menu Bar and Toolbar
Navigating the Visual Studio IDE
2.4.1 Solution Explorer
2.4.2 Toolbox
2.4.3 Properties Window
Using Help
Using Visual App Development to Create a Simple App that Displays
Text and an Image
Wrap-Up
Web Resources
3
Introduction to C# Apps
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
3.10
Introduction
A Simple C# App: Displaying a Line of Text
Creating a Simple App in Visual Studio
Modifying Your Simple C# App
Formatting Text with Console.Write and Console.WriteLine
Another C# App: Adding Integers
Memory Concepts
Arithmetic
Decision Making: Equality and Relational Operators
Wrap-Up
4
Introduction to Classes, Objects, Methods
and strings
2.5
2.6
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
4.11
4.12
Introduction
Classes, Objects, Methods, Properties and Instance Variables
Declaring a Class with a Method and Instantiating an Object of a Class
Declaring a Method with a Parameter
Instance Variables and Properties
UML Class Diagram with a Property
Software Engineering with Properties and set and get Accessors
Auto-Implemented Properties
Value Types vs. Reference Types
Initializing Objects with Constructors
Floating-Point Numbers and Type decimal
Wrap-Up
19
23
33
34
34
39
41
43
44
44
46
47
57
58
65
66
66
72
77
80
81
85
86
90
94
106
107
107
108
113
116
121
121
123
123
125
128
134
Contents
5
Control Statements: Part 1
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14
Introduction
Algorithms
Pseudocode
Control Structures
if Single-Selection Statement
if…else Double-Selection Statement
while Repetition Statement
Formulating Algorithms: Counter-Controlled Repetition
Formulating Algorithms: Sentinel-Controlled Repetition
Formulating Algorithms: Nested Control Statements
Compound Assignment Operators
Increment and Decrement Operators
Simple Types
Wrap-Up
6
Control Statements: Part 2
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
Introduction
Essentials of Counter-Controlled Repetition
for Repetition Statement
Examples Using the for Statement
do…while Repetition Statement
switch Multiple-Selection Statement
break and continue Statements
Logical Operators
Structured-Programming Summary
Wrap-Up
7
Methods: A Deeper Look
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
Introduction
Packaging Code in C#
static Methods, static Variables and Class Math
Declaring Methods with Multiple Parameters
Notes on Declaring and Using Methods
Method-Call Stack and Activation Records
Argument Promotion and Casting
The .NET Framework Class Library
Case Study: Random-Number Generation
7.9.1 Scaling and Shifting Random Numbers
7.9.2 Random-Number Repeatability for Testing and Debugging
Case Study: A Game of Chance; Introducing Enumerations
Scope of Declarations
Method Overloading
Optional Parameters
7.10
7.11
7.12
7.13
ix
142
143
143
144
144
146
148
152
154
158
166
171
171
174
175
189
190
190
191
195
199
201
209
211
216
221
231
232
232
234
236
240
241
242
243
245
249
250
250
255
258
260
x
Contents
7.14
7.15
7.16
7.17
Named Parameters
Recursion
Passing Arguments: Pass-by-Value vs. Pass-by-Reference
Wrap-Up
8
Arrays; Introduction to Exception Handling
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
8.9
8.10
8.11
8.12
8.13
8.14
Introduction
Arrays
Declaring and Creating Arrays
Examples Using Arrays
8.4.1 Creating and Initializing an Array
8.4.2 Using an Array Initializer
8.4.3 Calculating a Value to Store in Each Array Element
8.4.4 Summing the Elements of an Array
8.4.5 Using Bar Charts to Display Array Data Graphically
8.4.6 Using the Elements of an Array as Counters
8.4.7 Using Arrays to Analyze Survey Results; Introduction to
Exception Handling
Case Study: Card Shuffling and Dealing Simulation
foreach Statement
Passing Arrays and Array Elements to Methods
Passing Arrays by Value and by Reference
Case Study: GradeBook Using an Array to Store Grades
Multidimensional Arrays
Case Study: GradeBook Using a Rectangular Array
Variable-Length Argument Lists
Using Command-Line Arguments
Wrap-Up
9
Introduction to LINQ and the List Collection 351
9.1
9.2
9.3
9.4
9.5
9.6
9.7
Introduction
Querying an Array of int Values Using LINQ
Querying an Array of Employee Objects Using LINQ
Introduction to Collections
Querying a Generic Collection Using LINQ
Wrap-Up
Deitel LINQ Resource Center
10
Classes and Objects: A Deeper Look
10.1
10.2
10.3
10.4
10.5
Introduction
Time Class Case Study
Controlling Access to Members
Referring to the Current Object’s Members with the this Reference
Time Class Case Study: Overloaded Constructors
262
263
266
269
285
286
286
288
289
289
290
291
292
293
295
296
299
303
305
307
311
316
321
327
329
331
352
353
357
362
365
367
367
371
372
372
376
377
379
Contents
10.6
10.7
10.8
10.9
10.10
10.11
10.12
10.13
10.14
Default and Parameterless Constructors
Composition
Garbage Collection and Destructors
static Class Members
readonly Instance Variables
Data Abstraction and Encapsulation
Class View and Object Browser
Object Initializers
Wrap-Up
11
Object-Oriented Programming: Inheritance
11.1
11.2
11.3
11.4
Introduction
Base Classes and Derived Classes
protected Members
Relationship between Base Classes and Derived Classes
11.4.1 Creating and Using a CommissionEmployee Class
11.4.2 Creating a BasePlusCommissionEmployee Class without
Using Inheritance
11.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy
11.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Instance Variables
11.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy Using private Instance Variables
Constructors in Derived Classes
Software Engineering with Inheritance
Class object
Wrap-Up
11.5
11.6
11.7
11.8
12
12.1
12.2
12.3
12.4
12.5
OOP: Polymorphism, Interfaces and
Operator Overloading
Introduction
Polymorphism Examples
Demonstrating Polymorphic Behavior
Abstract Classes and Methods
Case Study: Payroll System Using Polymorphism
12.5.1 Creating Abstract Base Class Employee
12.5.2 Creating Concrete Derived Class SalariedEmployee
12.5.3 Creating Concrete Derived Class HourlyEmployee
12.5.4 Creating Concrete Derived Class CommissionEmployee
12.5.5 Creating Indirect Concrete Derived Class
BasePlusCommissionEmployee
12.5.6 Polymorphic Processing, Operator is and Downcasting
xi
385
386
389
390
393
394
396
398
398
405
406
407
409
410
410
415
420
423
428
433
434
434
435
441
442
444
445
448
450
451
453
455
457
458
460
xii
Contents
12.8
12.9
12.5.7 Summary of the Allowed Assignments Between Base-Class
and Derived-Class Variables
sealed Methods and Classes
Case Study: Creating and Using Interfaces
12.7.1 Developing an IPayable Hierarchy
12.7.2 Declaring Interface IPayable
12.7.3 Creating Class Invoice
12.7.4 Modifying Class Employee to Implement Interface IPayable
12.7.5 Modifying Class SalariedEmployee for Use with IPayable
12.7.6 Using Interface IPayable to Process Invoices and Employees
Polymorphically
12.7.7 Common Interfaces of the .NET Framework Class Library
Operator Overloading
Wrap-Up
13
Exception Handling: A Deeper Look
13.1
13.2
13.3
Introduction
Example: Divide by Zero without Exception Handling
Example: Handling DivideByZeroExceptions and FormatExceptions
13.3.1 Enclosing Code in a try Block
13.3.2 Catching Exceptions
13.3.3 Uncaught Exceptions
13.3.4 Termination Model of Exception Handling
13.3.5 Flow of Control When Exceptions Occur
.NET Exception Hierarchy
13.4.1 Class SystemException
13.4.2 Determining Which Exceptions a Method Throws
finally Block
The using Statement
Exception Properties
User-Defined Exception Classes
Wrap-Up
12.6
12.7
13.4
13.5
13.6
13.7
13.8
13.9
14
14.1
14.2
14.3
14.4
Graphical User Interfaces with
Windows Forms: Part 1
Introduction
Windows Forms
Event Handling
14.3.1 A Simple Event-Driven GUI
14.3.2 Auto-Generated GUI Code
14.3.3 Delegates and the Event-Handling Mechanism
14.3.4 Another Way to Create Event Handlers
14.3.5 Locating Event Information
Control Properties and Layout
465
466
466
468
469
469
471
473
474
476
477
480
486
487
488
491
493
493
494
495
495
496
496
497
497
504
505
509
513
518
519
520
522
522
524
526
527
528
529
Contents
14.5 Labels, TextBoxes and Buttons
14.6 GroupBoxes and Panels
14.7 CheckBoxes and RadioButtons
14.8 PictureBoxes
14.9 ToolTips
14.10 NumericUpDown Control
14.11 Mouse-Event Handling
14.12 Keyboard-Event Handling
14.13 Wrap-Up
15
Graphical User Interfaces with
Windows Forms: Part 2
15.1
15.2
15.3
15.4
15.5
15.6
15.7
15.8
15.9
15.10
15.11
15.12
15.13
15.14
15.15
Introduction
Menus
16
Strings and Characters: A Deeper Look
16.1
16.2
16.3
16.4
16.5
16.6
16.7
16.8
16.9
16.10
16.11
Control
Control
LinkLabel Control
ListBox Control
CheckedListBox Control
ComboBox Control
TreeView Control
ListView Control
TabControl Control
Multiple Document Interface (MDI) Windows
Visual Inheritance
User-Defined Controls
Wrap-Up
MonthCalendar
DateTimePicker
Introduction
Fundamentals of Characters and Strings
string Constructors
string Indexer, Length Property and CopyTo Method
Comparing strings
Locating Characters and Substrings in strings
Extracting Substrings from strings
Concatenating strings
Miscellaneous string Methods
Class StringBuilder
Length and Capacity Properties, EnsureCapacity Method and Indexer
of Class StringBuilder
16.12 Append and AppendFormat Methods of Class StringBuilder
16.13 Insert, Remove and Replace Methods of Class StringBuilder
16.14 Char Methods
xiii
533
536
539
547
549
551
553
556
559
569
570
570
579
580
583
587
591
594
598
603
609
614
621
626
630
638
639
640
641
642
643
646
649
650
651
652
653
655
657
660
xiv
Contents
16.15 (Online) Introduction to Regular Expressions
16.16 Wrap-Up
17
Files and Streams
17.1
17.2
17.3
17.4
17.5
17.6
17.7
17.8
17.9
17.10
17.11
Introduction
Data Hierarchy
Files and Streams
Classes File and Directory
Creating a Sequential-Access Text File
Reading Data from a Sequential-Access Text File
Case Study: Credit Inquiry Program
Serialization
Creating a Sequential-Access File Using Object Serialization
Reading and Deserializing Data from a Binary File
Wrap-Up
18
Searching and Sorting
18.1
18.2
18.4
18.5
Introduction
Searching Algorithms
18.2.1 Linear Search
18.2.2 Binary Search
Sorting Algorithms
18.3.1 Selection Sort
18.3.2 Insertion Sort
18.3.3 Merge Sort
Summary of the Efficiency of Searching and Sorting Algorithms
Wrap-Up
19
Data Structures
19.1
19.2
19.3
19.4
19.5
19.6
19.7
19.8
Introduction
Simple-Type structs, Boxing and Unboxing
Self-Referential Classes
Linked Lists
Stacks
Queues
Trees
19.7.1 Binary Search Tree of Integer Values
19.7.2 Binary Search Tree of IComparable Objects
Wrap-Up
20
Generics
20.1
20.2
20.3
Introduction
Motivation for Generic Methods
Generic-Method Implementation
18.3
662
663
669
670
670
672
673
682
691
695
701
702
706
708
715
716
717
717
721
726
726
730
734
740
741
746
747
747
748
749
762
766
769
770
777
782
789
790
791
793
Contents
20.4
20.5
20.6
20.7
Type Constraints
Overloading Generic Methods
Generic Classes
Wrap-Up
21
Collections
21.1
21.2
21.3
21.4
21.6
21.7
Introduction
Collections Overview
Class Array and Enumerators
Nongeneric Collections
21.4.1 Class ArrayList
21.4.2 Class Stack
21.4.3 Class Hashtable
Generic Collections
21.5.1 Generic Class SortedDictionary
21.5.2 Generic Class LinkedList
Covariance and Contravariance for Generic Types
Wrap-Up
22
Databases and LINQ
21.5
22.1
22.2
22.3
22.4
22.5
Introduction
Relational Databases
A Books Database
LINQ to Entities and the ADO.NET Entity Framework
Querying a Database with LINQ
22.5.1 Creating the ADO.NET Entity Data Model Class Library
22.5.2 Creating a Windows Forms Project and Configuring It to
Use the Entity Data Model
22.5.3 Data Bindings Between Controls and the Entity Data Model
22.6 Dynamically Binding Query Results
22.6.1 Creating the Display Query Results GUI
22.6.2 Coding the Display Query Results App
22.7 Retrieving Data from Multiple Tables with LINQ
22.8 Creating a Master/Detail View App
22.8.1 Creating the Master/Detail GUI
22.8.2 Coding the Master/Detail App
22.9 Address Book Case Study
22.9.1 Creating the Address Book App’s GUI
22.9.2 Coding the Address Book App
22.10 Tools and Web Resources
22.11 Wrap-Up
23
Web App Development with ASP.NET
23.1
Introduction
xv
796
798
799
808
814
815
815
818
821
821
826
828
833
834
836
840
843
849
850
851
852
856
857
858
862
864
869
870
871
874
879
880
881
883
884
885
889
889
897
898
xvi
Contents
23.2
23.3
23.4
Web Basics
Multitier App Architecture
Your First Web App
23.4.1 Building the WebTime App
23.4.2 Examining WebTime.aspx’s Code-Behind File
23.5 Standard Web Controls: Designing a Form
23.6 Validation Controls
23.7 Session Tracking
23.7.1 Cookies
23.7.2 Session Tracking with HttpSessionState
23.7.3 Options.aspx: Selecting a Programming Language
23.7.4 Recommendations.aspx: Displaying Recommendations
Based on Session Values
23.8 Case Study: Database-Driven ASP.NET Guestbook
23.8.1 Building a Web Form that Displays Data from a Database
23.8.2 Modifying the Code-Behind File for the Guestbook App
23.9 Online Case Study: ASP.NET AJAX
23.10 Online Case Study: Password-Protected Books Database App
23.11 Wrap-Up
899
900
902
904
913
914
918
925
926
927
928
932
933
935
940
941
942
942
Chapters on the Web
949
A
Operator Precedence Chart
950
B
Simple Types
952
C
ASCII Character Set
954
Appendices on the Web
955
Index
957
Chapters 24–35 and Appendices D–G are PDF documents posted online at the book’s
Companion Website (located at www.pearsonhighered.com/deitel/).
24
XML and LINQ to XML
25
Windows 8 UI and XAML
26
Windows 8 Graphics and Multimedia
Contents
xvii
27
Building a Windows Phone 8 App
28
Asynchronous Programming with async and await
29
Web App Development with ASP.NET: A Deeper
Look
30
Web Services
31
Building a Windows Azure™ Cloud
Computing App
32
GUI with Windows Presentation Foundation
33
WPF Graphics and Multimedia
34
ATM Case Study, Part 1: Object-Oriented Design
with the UML
35
ATM Case Study, Part 2: Implementing an ObjectOriented Design
D
Number Systems
E
UML 2: Additional Diagram Types
F
Unicode®
G
Using the Visual C# 2012 Debugger