SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Signature “Live Code™ Approach” – Language features are presented in the context of a wide variety of complete working programs.
Appropriate for all basic-to-intermediate level courses in Visual C# 2010 programming.
Created by world-renowned programming instructors Paul and Harvey Deitel, Visual C# 2010 How to Program, Fourth Edition introduces all facets of the C# 2010 language hands-on, through hundreds of working programs. This book has been thoroughly updated to reflect the major innovations Microsoft has incorporated in Visual C# 2010 and .NET 4; all discussions and sample code have been carefully audited against the newest Visual C# language specification.
Students begin by getting comfortable with the C# Express 2010 IDE and basic Visual C# syntax. Next, they build their skills one step at a time, mastering control structures, classes, objects, methods, variables, arrays, and the core techniques of object-oriented programming. With this strong foundation in place, the Deitels introduce more sophisticated techniques, including searching, sorting, data structures, generics, and collections. Throughout, the authors show students how to make the most of Microsoft’s Visual Studio tools. A series of appendices provide essential programming reference material on topics ranging from number systems to the Visual Studio Debugger, UML 2 to Unicode and ASCII.
Introduction to Classes, Objects, Methods and Strings in Visual C# 2010
Chapters 24—31 and Appendices D—G are PDF documents posted online at the book’s Companion Website (located at www.pearsonhighered.com/deitel/).
Preface xvii
Before You Begin xxvii
1 Introduction to Computers, the Internet and Visual C# 1
1.1 Introduction 2
1.2 Computer Organization 2
1.3 Personal Computing, Distributed Computing and Client/Server Computing 4
1.4 Hardware Trends 4
1.5 Microsoft’s Windows® Operating System 4
1.6 Machine Languages, Assembly Languages and High-Level Languages 5
1.7 Visual Basic 6
1.8 C, C++, Objective-C and Java 6
1.9 C# 7
1.10 The Internet and the World Wide Web 7
1.11 Extensible Markup Language (XML) 8
1.12 Introduction to Microsoft .NET 9
1.13 The .NET Framework and the Common Language Runtime 9
1.14 Test-Driving the Advanced Painter Application 10
1.15 Introduction to Object Technology 12
1.16 Wrap-Up 15
1.17 Web Resources 15
2 Dive Into® Visual C# 2010 Express 24
2.1 Introduction 25
2.2 Overview of the Visual Studio 2010 IDE 25
2.3 Menu Bar and Toolbar 30
2.4 Navigating the Visual Studio IDE 32
2.4.1 Solution Explorer 34
2.4.2 Toolbox 35
2.4.3 Properties Window 36
2.5 Using Help 37
2.6 Using Visual Programming to Create a Simple Program that Displays Text and an Image 40
2.7 Wrap-Up 51
2.8 Web Resources 52
3 Introduction to C# Applications 60
3.1 Introduction 61
3.2 A Simple C# Application: Displaying a Line of Text 61
3.3 Creating a Simple Application in Visual C# Express 66
3.4 Modifying Your Simple C# Application 74
3.5 Formatting Text with Console.Write and Console.WriteLine 76
3.6 Another C# Application: Adding Integers 77
3.7 Memory Concepts 81
3.8 Arithmetic 82
3.9 Decision Making: Equality and Relational Operators 85
3.10 Wrap-Up 90
4 Introduction to Classes, Objects, Methods and strings 101
4.1 Introduction 102
4.2 Classes, Objects, Methods, Properties and Instance Variables 102
4.3 Declaring a Class with a Method and Instantiating an Object of a Class 103
4.4 Declaring a Method with a Parameter 107
4.5 Instance Variables and Properties 111
4.6 UML Class Diagram with a Property 115
4.7 Software Engineering with Properties and set and get Accessors 116
4.8 Auto-Implemented Properties 117
4.9 Value Types vs. Reference Types 118
4.10 Initializing Objects with Constructors 119
4.11 Floating-Point Numbers and Type decimal 122
4.12 Wrap-Up 128
5 Control Statements: Part 1 136
5.1 Introduction 137
5.2 Algorithms 137
5.3 Pseudocode 138
5.4 Control Structures 138
5.5 if Single-Selection Statement 140
5.6 if…else Double-Selection Statement 141
5.7 while Repetition Statement 146
5.8 Formulating Algorithms: Counter-Controlled Repetition 147
5.9 Formulating Algorithms: Sentinel-Controlled Repetition 152
5.10 Formulating Algorithms: Nested Control Statements 160
5.11 Compound Assignment Operators 165
5.12 Increment and Decrement Operators 165
5.13 Simple Types 168
5.14 Wrap-Up 169
6 Control Statements: Part 2 183
6.1 Introduction 184
6.2 Essentials of Counter-Controlled Repetition 184
6.3 for Repetition Statement 186
6.4 Examples Using the for Statement 190
6.5 do…while Repetition Statement 194
6.6 switch Multiple-Selection Statement 196
6.7 break and continue Statements 203
6.8 Logical Operators 205
6.9 Structured-Programming Summary 211
6.10 Wrap-Up 216
7 Methods: A Deeper Look 226
7.1 Introduction 227
7.2 Packaging Code in C# 227
7.3 static Methods, static Variables and Class Math 229
7.4 Declaring Methods with Multiple Parameters 232
7.5 Notes on Declaring and Using Methods 236
7.6 Method-Call Stack and Activation Records 237
7.7 Argument Promotion and Casting 237
7.8 The .NET Framework Class Library 239
7.9 Case Study: Random-Number Generation 241
7.9.1 Scaling and Shifting Random Numbers 245
7.9.2 Random-Number Repeatability for Testing and Debugging 245
7.10 Case Study: A Game of Chance (Introducing Enumerations) 246
7.11 Scope of Declarations 251
7.12 Method Overloading 253
7.13 Optional Parameters 256
7.14 Named Parameters 257
7.15 Recursion 258
7.16 Passing Arguments: Pass-by-Value vs. Pass-by-Reference 261
7.17 Wrap-Up 264
8 Arrays 280
8.1 Introduction 281
8.2 Arrays 281
8.3 Declaring and Creating Arrays 282
8.4 Examples Using Arrays 284
8.5 Case Study: Card Shuffling and Dealing Simulation 293
8.6 foreach Statement 298
8.7 Passing Arrays and Array Elements to Methods 299
8.8 Passing Arrays by Value and by Reference 301
8.9 Case Study: Class GradeBook Using an Array to Store Grades 305
8.10 Multidimensional Arrays 310
8.11 Case Study: Class GradeBook Using a Rectangular Array 315
8.12 Variable-Length Argument Lists 321
8.13 Using Command-Line Arguments 322
8.14 Wrap-Up 324
9 Introduction to LINQ and the List Collection 344
9.1 Introduction 345
9.2 Querying an Array of int Values Using LINQ 346
9.3 Querying an Array of Employee Objects Using LINQ 350
9.4 Introduction to Collections 355
9.5 Querying a Generic Collection Using LINQ 358
9.6 Wrap-Up 360
9.7 Deitel LINQ Resource Center 360
10 Classes and Objects: A Deeper Look 364
10.1 Introduction 365
10.2 Time Class Case Study 365
10.3 Controlling Access to Members 369
10.4 Referring to the Current Object’s Members with the this Reference 370
10.5 Time Class Case Study: Overloaded Constructors 372
10.6 Default and Parameterless Constructors 378
10.7 Composition 379
10.8 Garbage Collection and Destructors 382
10.9 static Class Members 383
10.10 readonly Instance Variables 386
10.11 Data Abstraction and Encapsulation 388
10.12 Class View and Object Browser 389
10.13 Object Initializers 391
10.14 Wrap-Up 391
11 Object-Oriented Programming: Inheritance 398
11.1 Introduction 399
11.2 Base Classes and Derived Classes 400
11.3 protected Members 402
11.4 Relationship between Base Classes and Derived Classes 403
11.4.1 Creating and Using a CommissionEmployee Class 403
11.4.2 Creating a BasePlusCommissionEmployee Class without Using Inheritance 408
11.4.3 Creating a CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy 414
11.4.4 CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy Using protected Instance Variables 417
11.4.5 CommissionEmployee—BasePlusCommissionEmployee Inheritance Hierarchy Using private Instance Variables 421
11.5 Constructors in Derived Classes 426
11.6 Software Engineering with Inheritance 427
11.7 Class object 428
11.8 Wrap-Up 429
12 OOP: Polymorphism, Interfaces and Operator Overloading 435
12.1 Introduction 436
12.2 Polymorphism Examples 438
12.3 Demonstrating Polymorphic Behavior 439
12.4 Abstract Classes and Methods 442
12.5 Case Study: Payroll System Using Polymorphism 444
12.5.1 Creating Abstract Base Class Employee 445
12.5.2 Creating Concrete Derived Class SalariedEmployee 448
12.5.3 Creating Concrete Derived Class HourlyEmployee 449
12.5.4 Creating Concrete Derived Class CommissionEmployee 451
12.5.5 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee 452
12.5.6 Polymorphic Processing, Operator is and Downcasting 454
12.5.7 Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables 459
12.6 sealed Methods and Classes 460
12.7 Case Study: Creating and Using Interfaces 460
12.7.1 Developing an IPayable Hierarchy 462
12.7.2 Declaring Interface IPayable 463
12.7.3 Creating Class Invoice 463
12.7.4 Modifying Class Employee to Implement Interface IPayable 465
12.7.5 Modifying Class SalariedEmployee for Use with IPayable 466
12.7.6 Using Interface IPayable to Process Invoices and Employees Polymorphically 468
12.7.7 Common Interfaces of the .NET Framework Class Library 470
12.8 Operator Overloading 471
12.9 Wrap-Up 474
13 Exception Handling: A Deeper Look 479
13.1 Introduction 480
13.2 Example: Divide by Zero without Exception Handling 480
13.3 Example: Handling DivideByZeroExceptions and FormatExceptions 483
13.3.1 Enclosing Code in a try Block 485
13.3.2 Catching Exceptions 486
13.3.3 Uncaught Exceptions 486
13.3.4 Termination Model of Exception Handling 487
13.3.5 Flow of Control When Exceptions Occur 488
13.4 .NET Exception Hierarchy 488
13.4.1 Class SystemException 488
13.4.2 Determining Which Exceptions a Method Throws 489
13.5 finally Block 490
13.6 The using Statement 496
13.7 Exception Properties 497
13.8 User-Defined Exception Classes 502
13.9 Wrap-Up 505
14 Graphical User Interfaces with Windows Forms: Part 1 510
14.1 Introduction 511
14.2 Windows Forms 512
14.3 Event Handling 514
14.3.1 A Simple Event-Driven GUI 514
14.3.2 Visual Studio Generated GUI Code 516
14.3.3 Delegates and the Event-Handling Mechanism 518
14.3.4 Another Way to Create Event Handlers 519
14.3.5 Locating Event Information 519
14.4 Control Properties and Layout 521
14.5 Labels, TextBoxes and Buttons 525
14.6 GroupBoxes and Panels 528
14.7 CheckBoxes and RadioButtons 531
14.8 PictureBoxes 539
14.9 ToolTips 541
14.10 NumericUpDown Control 543
14.11 Mouse-Event Handling 545
14.12 Keyboard-Event Handling 548
14.13 Wrap-Up 551
15 Graphical User Interfaces with Windows Forms: Part 2 561
15.1 Introduction 562
15.2 Menus 562
15.3 MonthCalendar Control 571
15.4 DateTimePicker Control 572
15.5 LinkLabel Control 575
15.6 ListBox Control 579
15.7 CheckedListBox Control 583
15.8 ComboBox Control 586
15.9 TreeView Control 590
15.10 ListView Control 595
15.11 TabControl Control 601
15.12 Multiple Document Interface (MDI) Windows 606
15.13 Visual Inheritance 613
15.14 User-Defined Controls 618
15.15 Wrap-Up 622
16 Strings and Characters 630
16.1 Introduction 631
16.2 Fundamentals of Characters and Strings 632
16.3 string Constructors 633
16.4 string Indexer, Length Property and CopyTo Method 634
16.5 Comparing strings 635
16.6 Locating Characters and Substrings in strings 638
16.7 Extracting Substrings from strings 641
16.8 Concatenating strings 642
16.9 Miscellaneous string Methods 643
16.10 Class StringBuilder 644
16.11 Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder 645
16.12 Append and AppendFormat Methods of Class StringBuilder 647
16.13 Insert, Remove and Replace Methods of Class StringBuilder 649
16.14 Char Methods 652
16.15 (Online) Introduction to Regular Expressions 654
16.16 Wrap-Up 655
17 Files and Streams 661
17.1 Introduction 662
17.2 Data Hierarchy 662
17.3 Files and Streams 664
17.4 Classes File and Directory 665
17.5 Creating a Sequential-Access Text File 674
17.6 Reading Data from a Sequential-Access Text File 683
17.7 Case Study: Credit Inquiry Program 687
17.8 Serialization 693
17.9 Creating a Sequential-Access File Using Object Serialization 694
17.10 Reading and Deserializing Data from a Binary File 698
17.11 Wrap-Up 700
18 Databases and LINQ 707
18.1 Introduction 708
18.2 Relational Databases 709
18.3 A Books Database 710
18.4 LINQ to SQL 713
18.5 Querying a Database with LINQ 714
18.5.1 Creating LINQ to SQL Classes 715
18.5.2 Data Bindings Between Controls and the LINQ to SQL Classes 718
18.6 Dynamically Binding Query Results 722
18.6.1 Creating the Display Query Results GUI 723
18.6.2 Coding the Display Query Results Application 723
18.7 Retrieving Data from Multiple Tables with LINQ 725
18.8 Creating a Master/Detail View Application 731
18.8.1 Creating the Master/Detail GUI 732
18.8.2 Coding the Master/Detail Application 733
18.9 Address Book Case Study 736
18.9.1 Creating the Address Book Application’s GUI 738
18.9.2 Coding the Address Book Application 739
18.10 Tools and Web Resources 741
18.11 Wrap-Up 742
19 Web App Development with ASP.NET 748
19.1 Introduction 749
19.2 Web Basics 750
19.3 Multitier Application Architecture 751
19.4 Your First Web Application 753
19.4.1 Building the WebTime Application 755
19.4.2 Examining WebTime.aspx’s Code-Behind File 764
19.5 Standard Web Controls: Designing a Form 764
19.6 Validation Controls 769
19.7 Session Tracking 775
19.7.1 Cookies 776
19.7.2 Session Tracking with HttpSessionState 777
19.7.3 Options.aspx: Selecting a Programming Language 780
19.7.4 Recommendations.aspx: Displaying Recommendations Based on Session Values 783
19.8 Case Study: Database-Driven ASP.NET Guestbook 785
19.8.1 Building a Web Form that Displays Data from a Database 787
19.8.2 Modifying the Code-Behind File for the Guestbook Application 790
19.9 Online Case Study: ASP.NET AJAX 792
19.10 Online Case Study: Password-Protected Books Database Application 792
19.11 Wrap-Up 792
20 Searching and Sorting 799
20.1 Introduction 800
20.2 Searching Algorithms 801
20.2.1 Linear Search 801
20.2.2 Binary Search 805
20.3 Sorting Algorithms 810
20.3.1 Selection Sort 810
20.3.2 Insertion Sort 814
20.3.3 Merge Sort 818
20.4 Summary of the Efficiency of Searching and Sorting Algorithms 824
20.5 Wrap-Up 824
21 Data Structures 830
21.1 Introduction 831
21.2 Simple-Type structs, Boxing and Unboxing 831
21.3 Self-Referential Classes 832
21.4 Linked Lists 833
21.5 Stacks 846
21.6 Queues 850
21.7 Trees 853
21.7.1 Binary Search Tree of Integer Values 854
21.7.2 Binary Search Tree of IComparable Objects 861
21.8 Wrap-Up 866
22 Generics 873
22.1 Introduction 874
22.2 Motivation for Generic Methods 875
22.3 Generic-Method Implementation 877
22.4 Type Constraints 880
22.5 Overloading Generic Methods 882
22.6 Generic Classes 883
22.7 Wrap-Up 892
23 Collections 898
23.1 Introduction 899
23.2 Collections Overview 899
23.3 Class Array and Enumerators 902
23.4 Nongeneric Collections 905
23.4.1 Class ArrayList 905
23.4.2 Class Stack 909
23.4.3 Class Hashtable 912
23.5 Generic Collections 917
23.5.1 Generic Class SortedDictionary 917
23.5.2 Generic Class LinkedList 919
23.6 Covariance and Contravariance for Generic Types 923
23.7 Wrap-Up 925
Chapters on the Web 932
A Operator Precedence Chart 933
B Simple Types 935
C ASCII Character Set 937
Appendices on the Web 938
Index 939
Chapters 24—31 and Appendices D—G are PDF documents posted online at the book’s Companion Website (located at www.pearsonhighered.com/deitel/).
24 GUI with Windows Presentation Foundation
25 WPF Graphics and Multimedia
26 XML and LINQ to XML
27 Web App Development with ASP.NET: A Deeper Look
28 Windows Communication Foundation (WCF) Web Services
29 Silverlight and Rich Internet Applications
30 ATM Case Study, Part 1: Object-Oriented Design with the UML
31 ATM Case Study, Part 2: Implementing an Object-Oriented Design
D Number Systems
E UML 2: Additional Diagram Types
F Unicode®
G Using the Visual C# 2010 Debugger