0% found this document useful (0 votes)
4 views

Contents

Uploaded by

Hayba Taffese
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Contents

Uploaded by

Hayba Taffese
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

C# PROGRAMMING:

FROM PROBLEM ANALYSIS TO PROGRAM DESIGN

FOURTH EDITION

BARBARA DOYLE

Australia l Brazil l Japan l Korea l Mexico l Singapore l Spain l United Kingdom l United States

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
B RIEF C ONTENTS

Neale Cousland / Shutterstock.com

PREFACE xxi

1. Introduction to Computing and Programming 1

2. Data Types and Expressions 65

3. Methods and Behaviors 131

4. Creating Your Own Classes 191

5. Making Decisions 247

6. Repeating Instructions 313

7. Arrays 383

8. Advanced Collections 439

9. Introduction to Windows Programming 493

10. Programming Based on Events 577

11. Advanced Object-Oriented Programming Features 691

12. Debugging and Handling Exceptions 775

13. Working with Files 837

14. Working with Databases 893

15. Web-Based Applications 979

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
vi | C# Programming: From Problem Analysis to Program Design, Fourth Edition

APPENDIX A Visual Studio Configuration 1083

APPENDIX B Code Editor Tools 1099

APPENDIX C Character Sets 1111

APPENDIX D Operator Precedence 1113

APPENDIX E C# Keywords 1115

GLOSSARY 1117

INDEX 1131

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
TABLE OF C ONTENTS

Neale Cousland / Shutterstock.com

Preface xxi

INTRODUCTION TO COMPUTING AND PROGRAMMING 1


1 History of Computers 2
System and Application Software 4
System Software 4
Application Software 6
Software Development Process 6
Steps in the Program Development Process 7
Programming Methodologies 13
Structured Procedural Programming 14
Object-Oriented Programming 16
Evolution of C# and .NET 19
Programming Languages 19
.NET 21
Why C#? 23
Types of Applications Developed with C# 24
Web Applications 24
Windows Applications 25
Console Applications 26
Exploring the First C# Program 27
Elements of a C# Program 28
Comments 28
Using Directive 30
Namespace 32
Class Definition 32

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
viii | C# Programming: From Problem Analysis to Program Design, Fourth Edition

Main( ) Method 33
Method Body Statements 34
Compiling, Building, and Running an Application 38
Typing Your Program Statements 38
Compilation and Execution Process 39
Compiling the Source Code Using Visual Studio IDE 39
Debugging an Application 45
Syntax Errors 45
Run-time Errors 47
Creating an Application 47
Coding Standards 52
Pseudocode 52
Resources 53
Quick Review 53
Exercises 56
Programming Exercises 61

DATA TYPES AND EXPRESSIONS 65


2 Data Representation 66
Bits 66
Bytes 66
Binary Numbering System 66
Character Sets 69
Kilobyte, Megabyte, Gigabyte, Terabyte, Petabyte. . . 70
Memory Locations for Data 70
Identifiers 71
Variables 75
Literal Values 75
Types, Classes, and Objects 76
Types 76
Classes 77
Objects 78
Predefined Data Types 79
Value Types 80

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents | ix

Integral Data Types 82


Floating-Point Types 85
Decimal Types 86
Boolean Variables 87
Declaring Strings 88
Making Data Constant 88
Assignment Statements 89
Basic Arithmetic Operations 92
Increment and Decrement Operations 95
Compound Operations 98
Order of Operations 100
Mixed Expressions 102
Casts 103
Formatting Output 104
Width Specifier 109
Coding Standards 119
Naming Conventions 119
Spacing Conventions 119
Declaration Conventions 120
Resources 120
Quick Review 120
Exercises 121
Programming Exercises 127

METHODS AND BEHAVIORS 131


3 Anatomy of a Method 132
Modifiers 134
Return Type 137
Method Name 138
Parameters 138
Method Body 139
Calling Class Methods 141
Predefined Methods 143
Writing Your Own Class Methods 157
Void Methods 157
Value-Returning Method 159
Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
x | C# Programming: From Problem Analysis to Program Design, Fourth Edition

Types of Parameters 164


Named and Optional Parameters 169
Default Values with Optional Parameters 170
Named Parameters 171
Coding Standards 180
Naming Conventions 180
Spacing Conventions 180
Declaration Conventions 180
Commenting Conventions 181
Resources 181
Quick Review 181
Exercises 182
Programming Exercises 189

CREATING YOUR OWN CLASSES 191


4 The Object Concept 192
Private Member Data 193
Constructor 197
Writing Your Own Instance Methods 200
Accessor 200
Mutators 201
Other Instance Methods 202
Property 202
ToString( ) Method 204
Calling Instance Methods 206
Calling the Constructor 206
Calling Accessor and Mutator Methods 208
Calling Other Instance Methods 209
Testing Your New Class 210
Coding Standards 235
Naming Conventions 235
Classes 235
Properties 235
Methods 236

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents | xi

Constructor Guidelines 236


Spacing Conventions 236
Resources 236
Quick Review 237
Exercises 238
Programming Exercises 244

MAKING DECISIONS 247


5 Boolean Expressions 248
Boolean Results 248
Conditional Expressions 249
Equality, Relational, and Logical Tests 250
Short-Circuit Evaluation 258
Boolean Data Type 260
if. . .else Selection Statements 261
One-Way if Statement 261
Two-Way if Statement 266
Nested if. . .else Statement 273
Switch Selection Statements 279
Ternary Operator ? : 283
Order of Operations 285
Coding Standards 298
Guidelines for Placement of Curly Braces 299
Guidelines for Placement of else with Nested if Statements 299
Guidelines for Use of White Space with a Switch Statement 299
Spacing Conventions 300
Advanced Selection Statement Suggestions 300
Resources 300
Quick Review 301
Exercises 302
Programming Exercises 310

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xii | C# Programming: From Problem Analysis to Program Design, Fourth Edition

REPEATING INSTRUCTIONS 313


6 Why Use a Loop? 314
Using the While Statement 314
Counter-Controlled Loop 316
Sentinel-Controlled Loop 321
State-Controlled Loops 332
Using the for Statement Loop 335
Using the Foreach Statement 342
Using the Do...while Structure 343
Nested Loops 346
Recursive Calls 351
Unconditional Transfer of Control 354
Continue Statement 355
Deciding Which Loop to Use 356
Coding Standards 370
Guidelines for Placement of Curly Braces 371
Spacing Conventions 371
Advanced Loop Statement Suggestions 371
Resources 372
Quick Review 372
Exercises 373
Programming Exercises 379

ARRAYS 383
7 Array Basics 384
Array Declaration 385
Array Initializers 388
Array Access 390
Sentinel-Controlled Access 394
Using Foreach with Arrays 395
Array Class 396
Arrays as Method Parameters 401
Pass by Reference 401
Array Assignment 405
Params Parameters 406

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents | xiii

Arrays in Classes 408


Array of User-Defined Objects 410
Arrays as Return Types 410
Coding Standards 428
Guidelines for Naming Arrays 428
Advanced Array Suggestions 428
Resources 429
Quick Review 429
Exercises 430
Programming Exercises 437

ADVANCED COLLECTIONS 439


8 Two-Dimensional Arrays 440
Rectangular Array 440
Jagged Array 450
Multidimensional Arrays 450
ArrayList Class 455
String Class 459
Other Collection Classes 465
BitArray 466
Hashtable 467
Queue 469
Stack 470
Coding Standards 479
Guidelines for Naming Collections 479
Advanced Array Suggestions 479
Resources 479
Quick Review 480
Exercises 481
Programming Exercises 488

INTRODUCTION TO WINDOWS PROGRAMMING 493


9 Contrasting Windows and Console Applications 494
Graphical User Interfaces 496

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xiv | C# Programming: From Problem Analysis to Program Design, Fourth Edition

Elements of Good Design 500


Consistency 500
Alignment 500
Avoid Clutter 501
Color 501
Target Audience 501
Using C# and Visual Studio to Create Windows-Based
Applications 502
Windows Forms 505
Windows Forms Properties 505
Inspecting the Code Generated by Visual Studio 511
Windows Forms Events 515
Controls 517
Placing, Moving, Resizing, and Deleting Control Objects 520
Methods and Properties of the Control Class 521
Derived Classes of the System.Windows.Forms.Control Class 524
Coding Standards 567
Guidelines for Naming Controls 567
Resources 567
Quick Review 568
Exercises 569
Programming Exercises 574

PROGRAMMING BASED ON EVENTS 577


10 Delegates 578
Defining Delegates 578
Creating Delegate Instances 579
Using Delegates 580
Relationship of Delegates to Events 583
Event Handling in C# 583
Event-Handler Methods 584
ListBox Control Objects 585
Creating a Form to Hold ListBox Controls 585
ListBox Event Handlers 588
Multiple Selections with a ListBox Object 589
Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents | xv

ComboBox Control Objects 601


Adding ComboBox Objects 602
Handling ComboBox Events 602
Registering a KeyPress Event 603
Programming Event Handlers 603
MenuStrip Control Objects 605
Adding Menus 606
Adding Predefined Standard Windows Dialog Boxes 610
CheckBox and RadioButton Objects 618
CheckBox Objects 618
Adding CheckBox Objects 619
Registering CheckBox Object Events 619
Wiring One Event Handler to Multiple Objects 621
GroupBox Objects 622
RadioButton Objects 622
Adding RadioButton Objects 622
Registering RadioButton Object Events 624
Windows Presentation Foundation (WPF) 633
TabControl Objects 639
Coding Standards 680
Resources 680
Quick Review 680
Exercises 682
Programming Exercises 688

ADVANCED OBJECT-ORIENTED PROGRAMMING


11 FEATURES 691
Object-Oriented Language Features 692
Component-Based Development 693
Inheritance 694
Inheriting from the Object Class 695
Inheriting from Other .NET FCL Classes 695
Creating Base Classes for Inheritance 696
Overriding Methods 699
Creating Derived Classes 700
Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xvi | C# Programming: From Problem Analysis to Program Design, Fourth Edition

Making Stand-Alone Components 705


Creating a Client Application to Use the DLL 715
Using ILDASM to View the Assembly (Optional) 719
Abstract Classes 721
Abstract Methods 721
Sealed Classes 724
Sealed Methods 725
Partial Classes 725
Creating Partial Classes 726
Interfaces 726
Defining an Interface 727
Implementing the Interface 728
.NET Framework Interfaces 733
Polymorphism 734
Polymorphic Programming in .NET 736
Generics 737
Generic Classes 737
Generic Methods 741
Dynamic 742
Dynamic data type 743
var data type 744
Coding Standards 764
Resources 765
Quick Review 765
Exercises 767
Programming Exercises 772

DEBUGGING AND HANDLING EXCEPTIONS 775


12 Errors 776
Run-Time Errors 777
Debugging in C# 778
Exceptions 786
Raising an Exception 790
Bugs, Errors, and Exceptions 791

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents | xvii

Exception-Handling Techniques 793


Try. . .Catch. . .Finally Blocks 794
Exception Object 798
Exception Classes 799
Derived Classes of the Base Exception Class 799
ApplicationException Class 800
SystemException Class 801
Filtering Multiple Exceptions 802
Throwing an Exception 809
Input Output (IO) Exceptions 810
Coding Standards 828
Resources 829
Quick Review 829
Exercises 830
Programming Exercises 835

WORKING WITH FILES 837


13 System.IO Namespace 838
File and Directory Classes 840
File Class 840
Directory Class 844
FileInfo and DirectoryInfo Classes 845
File Streams 848
Writing Text Files 851
Reading Text Files 856
Adding a Using Statement 860
Random Access 863
BinaryReader and BinaryWriter Classes 863
Other Stream Classes 869
FileDialog Class 870
Coding Standards 884
Resources 884
Quick Review 884
Exercises 885
Programming Exercises 890

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xviii | C# Programming: From Problem Analysis to Program Design, Fourth Edition

WORKING WITH DATABASES 893


14 Database Access 894
Database Management Systems 894
ADO.NET 895
Data Providers 896
Connecting to the Database 899
Retrieving Data from the Database 901
Processing the Data 905
Updating Database Data 913
Using Datasets to Process Database Records 913
Data Source Configuration Tools 921
Add New Data Source 921
Dataset Object 931
TableAdapterManager 941
DataSet Designer 942
Connecting Multiple Tables 953
Displaying Data Using Details View 959
Language-Integrated Query (LINQ) 962
Query Expressions 963
Implicitly Typed Local Variables 966
LINQ with Databases 966
LINQ to SQL 968
Coding Standards 969
Resources 969
Quick Review 969
Exercises 971
Programming Exercises 976

WEB-BASED APPLICATIONS 979


15 Web-Based Applications 980
Web Programming Model 980
Static Pages 981
Dynamic Pages 984

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents | xix

ASP.NET 986
Visual Studio for Web Development 986
ASP.NET Programming Models 987
Web Forms Page 988
Creating an ASP.NET Web Forms Site 988
Master Pages 993
Cascading Style Sheet (CSS) 997
ASP.NET Empty Web Site 1001
Controls 1004
HTML Controls 1004
HTML Server Controls 1008
Web Forms Standard Server Controls 1012
Available Web Forms Controls 1012
Web Forms Controls of the Common Form Type 1014
Adding Common Form-Type Controls 1018
Validation, Custom, and Composite Controls 1021
Validation Controls 1021
Calendar Control 1026
GridView Control 1033
AccessDataSource 1039
Using Visual Tools to Connect 1040
Setting the Visibility Property 1045
Other Controls 1047
Web Services 1050
Web Services Protocols 1050
Windows Communication Foundation (WCF) 1052
Smart Device Applications (Optional) 1052
Windows 7.x Phone 1053
Silverlight 1054
Creating a Smart Device Application for Windows 7.x Phones 1054
Windows 8 Phone Apps 1061
Creating a Windows 8 Phone App 1063
XML 1064
Code-Behind File 1065
XAML Code 1067
Running the App 1068
Deploying to an Emulator 1068
Deploying to a Device 1071
Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
xx | C# Programming: From Problem Analysis to Program Design, Fourth Edition

Coding Standards 1073


Resources 1074
Quick Review 1075
Exercises 1076
Programming Exercises 1081

APPENDIX A: VISUAL STUDIO CONFIGURATION 1083


Customizing the Development Environment 1083
Environment 1085
Projects and Solutions 1088
Text Editor 1089
Debugging 1093
HTML Designer 1093
Windows Forms Designer 1094
Other Options Settings 1095
Choose Toolbox Items 1096
Customize the Toolbars 1097

APPENDIX B: CODE EDITOR TOOLS 1099


Code Snippets 1099
Refactoring 1101
Extrace Method 1102
Rename 1104
Other Refactoring Options 1105
Working with Class Diagrams 1106
Class Details View 1107
Using the Class Diagram to Add Members 1108
Other Code Editor Tips 1109

APPENDIX C: CHARACTER SETS 1111

APPENDIX D: OPERATOR PRECEDENCE 1113

APPENDIX E: C# KEYWORDS 1115

GLOSSARY 1117

INDEX 1131

Copyright 2013 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

You might also like