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.
The professional programmer’s Deitel® guide to C# and the powerful Microsoft® .NET Framework
Written for programmers with a background in C++, Java or other high-level languages, this book applies the Deitel signature live-code approach to teaching programming and explores Microsoft’s C# language and .NET Framework 3.5 in depth. The book is updated for Visual Studio® 2008 and C# 3.0, and presents C# concepts in the context of fully tested programs, complete with syntax shading, code highlighting, line-by-line code descriptions, and program outputs. The book features 200+ C# applications with about 20,000 lines of proven C# code, and hundreds of tips that will help you build robust applications.
Start with a concise introduction to C# using an early classes and objects approach, then rapidly move on to more advanced topics, including the .NET Framework 3.5, LINQ, WPF, ASP.NET AJAX, WCF web services and Silverlight™. You’ll enjoy the Deitels’ classic treatment of object-oriented programming and the OOD/UML™ ATM case study, including a complete C# implementation. When you’re finished, you’ll have everything you need to build next-generation Windows applications, web applications and web services.
TheDeitel® Developer Series isdesigned for practicing programmers. The series presents focused treatments of emerging technologies, including .NET, Java™, web services, Internet and web development, and more.
Practical, example-rich coverage of:
Visit www.deitel.com to:
Pre-Publication Reviewer Testimonials
“The ultimate, comprehensive book that teaches you how to program using the latest Microsoft technologies. Excellent explanations, lots of examples, all the necessary theoretical background and all the latest technologies for desktop, web and databases. The best overview of Silverlight.“
–Kirill Osenkov, Microsoft
“An excellent, true objects-first book. Excellent introduction to collections. The generics material is a real asset.“
–Gavin Osborne, Saskatchewan Institutue of Applied Science & Technology
“The early Introduction to Classes and Objects is brilliant. Coverage of ASP.NET 3.5 is exceptional. Includes a great introduction to ASP.NET AJAX.“
–José Antonio González Seco, Parliament of Andalusia
“Great chapter on polymorphism.“
–Eric Lippert, Microsoft
“Illustrates the best practices of C# programming. Teaches how to ‘program in the large,’ with material on object-oriented programming and software engineering principles.“
–Mingsheng Hong, Cornell University
“Excellent introduction to the world of .NET, using the Deitels’ live-code approach and real-world examples.“
–Bonnie Berent, Microsoft C# MVP
“Excellent chapter on exceptions. Very good chapter on Winforms GUIs.“
–Marcelo Guerra Hahn, Microsoft
“Perfect for professionals. Thorough introductions to the debugger and LINQ.“
–Vinay Ahuja, Microsoft
“The TV/video viewer will enthuse readers and help them see how complex graphics effects can be created easily in WPF. A good example of data binding in WPF.“
—Ged Mead, Microsoft MVP, DevCity.Net
“An excellent introduction to XML, LINQ to XML and related technologies.“
—Helena Kotas, Microsoft
“Good overview of relational databases. It hits on the right LINQ to SQL idioms.“
—Alex Turner, Microsoft
“Great overview of producing and consuming web services with WCF.“
—Dan Crevier, Microsoft
Download all the examples in one convenient file (354 MB)
Download the examples for Chapter 1
Download the examples for Chapter 2
Download the examples for Chapter 3
Download the examples for Chapter 4
Download the examples for Chapter 5
Download the examples for Chapter 6
Download the examples for Chapter 7
Download the examples for Chapter 8
Download the examples for Chapter 9
Download the examples for Chapter 10
Download the examples for Chapter 11
Download the examples for Chapter 12
Download the examples for Chapter 13
Download the examples for Chapter 14
Download the examples for Chapter 15
Download the examples for Chapter 16
Download the examples for Chapter 17
Download the examples for Chapter 18
Download the examples for Chapter 19
Download the examples for Chapter 20
Download the examples for Chapter 21
Download the examples for Chapter 22
Download the examples for Chapter 23
Download the examples for Chapter 24
Download the examples for Chapter 25
Download the examples for Chapter 26
Download the examples for Chapter 27
Download the examples for Appendix D
Download the examples for Appendix G
Download the examples for Appendix H
C# 2008 for Programmers: Databases and LINQ to SQL
Preface xxiii
Before You Begin xxxix
1 Introduction 1
1.1 Introduction 2
1.2 Microsoft’s Windows® Operating System 2
1.3 C, C++ and Java 3
1.4 Visual C# 4
1.5 Key Software Trend: Object Technology 5
1.6 The Internet and the World Wide Web 5
1.7 Extensible Markup Language (XML) 7
1.8 Introduction to Microsoft .NET 7
1.9 The .NET Framework and the Common Language Runtime 8
1.10 Test-Driving a C# Advanced Painter Application 9
1.11 (Only Required Section of the Case Study) Software Engineering Case Study: Introduction to Object Technology and the UML 11
1.12 Wrap-Up 16
1.13 Web Resources 16
2 Dive Into® Visual C# 2008 Express 18
2.1 Introduction 19
2.2 Overview of the Visual Studio 2008 IDE 19
2.3 Menu Bar and Toolbar 25
2.4 Navigating the Visual Studio IDE 28
2.5 Using Help 33
2.6 Using Visual Programming to Create a Simple Program that Displays Text and an Image 35
2.7 Wrap-Up 47
2.8 Web Resources 48
3 Introduction to C# Applications 49
3.1 Introduction 50
3.2 A Simple C# Application: Displaying a Line of Text 50
3.3 Creating a Simple Application in Visual C# Express 55
3.4 Modifying Your Simple C# Application 63
3.5 Formatting Text with Console.Write and Console.WriteLine 65
3.6 Another C# Application: Adding Integers 66
3.7 Arithmetic 70
3.8 Decision Making: Equality and Relational Operators 71
3.9 (Optional) Software Engineering Case Study: Examining the ATM Requirements Document 76
3.10 Wrap-Up 85
3.11 Web Resources 85
4 Introduction to Classes and Objects 86
4.1 Introduction 87
4.2 Classes, Objects, Methods, Properties and Instance Variables 87
4.3 Declaring a Class with a Method and Instantiating an Object of a Class 89
4.4 Declaring a Method with a Parameter 93
4.5 Instance Variables and Properties 96
4.6 UML Class Diagram with a Property 102
4.7 Software Engineering with Properties and set and get Accessors 102
4.8 Auto-Implemented Properties 104
4.9 Value Types vs. Reference Types 105
4.10 Initializing Objects with Constructors 107
4.11 Floating-Point Numbers and Type decimal 110
4.12 (Optional) Software Engineering Case Study: Identifying the Classes in the ATM Requirements Document 116
4.13 Wrap-Up 123
5 Control Statements: Part 1 125
5.1 Introduction 126
5.2 Control Structures 126
5.3 if Single-Selection Statement 129
5.4 if…else Double-Selection Statement 130
5.5 while Repetition Statement 134
5.6 Counter-Controlled Repetition 135
5.7 Formulating Algorithms: Sentinel-Controlled Repetition 139
5.8 Formulating Algorithms: Nested Control Statements 144
5.9 Compound Assignment Operators 147
5.10 Increment and Decrement Operators 148
5.11 Simple Types 151
5.12 (Optional) Software Engineering Case Study: Identifying Class Attributes in the ATM System 151
5.13 Wrap-Up 156
6 Control Statements: Part 2 157
6.1 Introduction 158
6.2 Essentials of Counter-Controlled Repetition 158
6.3 for Repetition Statement 160
6.4 Examples Using the for Statement 164
6.5 do…while Repetition Statement 168
6.6 switch Multiple-Selection Statement 170
6.7 break and continue Statements 178
6.8 Logical Operators 180
6.9 (Optional) Software Engineering Case Study: Identifying Objects’ States and Activities in the ATM System 186
6.10 Wrap-Up 190
7 Methods: A Deeper Look 192
7.1 Introduction 193
7.2 Packaging Code in C# 193
7.3 static Methods, static Variables and Class Math 194
7.4 Declaring Methods with Multiple Parameters 197
7.5 Notes on Declaring and Using Methods 201
7.6 Method-Call Stack and Activation Records 202
7.7 Argument Promotion and Casting 203
7.8 The .NET Framework Class Library 205
7.9 Case Study: Random-Number Generation 206
7.13 Recursion 223
7.14 Passing Arguments: Pass-by-Value vs. Pass-by-Reference 226
7.15 (Optional) Software Engineering Case Study: Identifying Class Operations in the ATM System 229
7.16 Wrap-Up 237
8 Arrays 238
8.1 Introduction 239
8.2 Arrays 239
8.3 Declaring and Creating Arrays 241
8.4 Examples Using Arrays 242
8.5 Case Study: Card Shuffling and Dealing Simulation 251
8.6 foreach Statement 255
8.7 Passing Arrays and Array Elements to Methods 257
8.8 Passing Arrays by Value and by Reference 259
8.9 Case Study: Class GradeBook Using an Array to Store Grades 263
8.10 Multidimensional Arrays 268
8.11 Case Study: Class GradeBook Using a Rectangular Array 273
8.12 Variable-Length Argument Lists 279
8.13 Using Command-Line Arguments 280
8.14 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System 282
8.15 Wrap-Up 290
9 Introduction to LINQ and Generic Collections 292
9.1 Introduction 293
9.2 Querying an Array Using LINQ 294
9.3 Introduction to Collections 303
9.4 Querying a Generic Collection Using LINQ 306
9.5 Wrap-Up 308
9.6 Deitel LINQ Resource Center 308
10 Classes and Objects: A Deeper Look 309
10.1 Introduction 310
10.2 Time Class Case Study 311
10.3 Controlling Access to Members 315
10.4 Referring to the Current Object’s Members with the this Reference 316
10.5 Indexers 318
10.6 Time Class Case Study: Overloaded Constructors 321
10.7 Default and Parameterless Constructors 327
10.8 Composition 328
10.9 Garbage Collection and Destructors 331
10.10 static Class Members 332
10.11 readonly Instance Variables 336
10.12 Software Reusability 338
10.13 Data Abstraction and Encapsulation 339
10.14 Time Class Case Study: Creating Class Libraries 341
10.15 internal Access 345
10.16 Class View and Object Browser 347
10.17 Object Initializers 348
10.18 Time Class Case Study: Extension Methods 351
10.19 Delegates 354
10.20 Lambda Expressions 357
10.21 Anonymous Types 360
10.22 (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System 362
10.23 Wrap-Up 368
11 Object-Oriented Programming: Inheritance 370
11.1 Introduction 371
11.2 Base Classes and Derived Classes 372
11.3 protected Members 374
11.4 Relationship between Base Classes and Derived Classes 375
11.5 Constructors in Derived Classes 399
11.6 Software Engineering with Inheritance 405
11.7 Class object 406
11.8 Wrap-Up 407
12 Polymorphism, Interfaces and Operator Overloading 408
12.1 Introduction 409
12.2 Polymorphism Examples 411
12.3 Demonstrating Polymorphic Behavior 412
12.4 Abstract Classes and Methods 415
12.5 Case Study: Payroll System Using Polymorphism 417
12.6 sealed Methods and Classes 432
12.7 Case Study: Creating and Using Interfaces 433
12.8 Operator Overloading 443
12.9 (Optional) Software Engineering Case Study: Incorporating Inheritance and Polymorphism into the ATM System 446
12.10 Wrap-Up 455
13 Exception Handling 456
13.1 Introduction 457
13.2 Exception-Handling Overview 458
13.3 Example: Divide by Zero without Exception Handling 458
13.4 Example: Handling DivideByZeroExceptions and FormatExceptions 461
13.5 .NET Exception Hierarchy 466
13.6 finally Block 468
13.7 Exception Properties 476
13.8 User-Defined Exception Classes 481
13.9 Wrap-Up 484
14 Graphical User Interfaces with Windows Forms: Part 1 485
14.1 Introduction 486
14.2 Windows Forms 487
14.3 Event Handling 490
14.4 Control Properties and Layout 497
14.5 Labels, TextBoxes and Buttons 500
14.6 GroupBoxes and Panels 503
14.7 CheckBoxes and RadioButtons 507
14.8 PictureBoxes 515
14.9 ToolTips 518
14.10 NumericUpDown Control 520
14.11 Mouse-Event Handling 522
14.12 Keyboard-Event Handling 525
14.13 Wrap-Up 528
15 Graphical User Interfaces with Windows Forms: Part 2 530
15.1 Introduction 531
15.2 Menus 531
15.3 MonthCalendar Control 541
15.4 DateTimePicker Control 542
15.5 LinkLabel Control 545
15.6 ListBox Control 549
15.7 CheckedListBox Control 553
15.8 ComboBox Control 556
15.9 TreeView Control 560
15.10 ListView Control 565
15.11 TabControl Control 571
15.12 Multiple Document Interface (MDI) Windows 576
15.13 Visual Inheritance 584
15.14 User-Defined Controls 587
15.15 Wrap-Up 592
16 GUI with Windows Presentation Foundation 593
16.1 Introduction 594
16.2 Windows Presentation Foundation (WPF) 595
16.3 XML Basics 596
16.4 Structuring Data 599
16.5 XML Namespaces 604
16.6 Declarative GUI Programming Using XAML 608
16.7 Creating a WPF Application in Visual C# Express 610
16.8 Laying Out Controls 612
16.15 Data-Driven GUIs with Data Binding 644
16.16 Wrap-Up 650
16.17 Web Resources 650
17 WPF Graphics and Multimedia 651
17.1 Introduction 652
17.2 Controlling Fonts 652
17.3 Basic Shapes 654
17.4 Polygons and Polylines 656
17.5 Brushes 659
17.6 Transforms 665
17.7 WPF Customization: A Television GUI 668
17.8 Animations 677
17.9 (Optional) 3-D Objects and Transforms 680
17.10 Wrap-Up 687
18 Strings, Characters and Regular Expressions 688
18.1 Introduction 689
18.2 Fundamentals of Characters and Strings 690
18.3 string Constructors 691
18.4 string Indexer, Length Property and CopyTo Method 692
18.5 Comparing strings 693
18.6 Locating Characters and Substrings in strings 697
18.7 Extracting Substrings from strings 699
18.8 Concatenating strings 700
18.9 Miscellaneous string Methods 701
18.10 Class StringBuilder 702
18.11 Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder 704
18.12 Append and AppendFormat Methods of Class StringBuilder 705
18.13 Insert, Remove and Replace Methods of Class StringBuilder 708
18.14 Char Methods 710
18.15 Card Shuffling and Dealing Simulation 713
18.16 Introduction to Regular-Expression Processing 717
18.17 Wrap-Up 731
19 Files and Streams 732
19.1 Introduction 733
19.2 Data Hierarchy 733 19.3 Files and Streams
19.4 Classes File and Directory
19.5 Creating a Sequential-Access Text File
19.6 Reading Data from a Sequential-Access Text File
19.7 Case Study: Credit Inquiry Program Using LINQ
19.8 Serialization
19.9 Creating a Sequential-Access File Using Object Serialization
19.10 Reading and Deserializing Data from a Binary File
19.11 Wrap-Up
20 XML and LINQ to XML
20.1 Introduction
20.2 Document Type Definitions (DTDs)
20.3 W3C XML Schema Documents
20.4 Extensible Stylesheet Language and XSL Transformations
20.5 LINQ to XML: Document Object Model (DOM)
20.6 LINQ to XML Class Hierarchy
20.7 LINQ to XML: Namespaces and Creating Documents
20.8 XSLT with Class XslCompiledTransform
20.9 Wrap-Up
20.10 Web Resources
21 Databases and LINQ to SQL
21.1 Introduction
21.2 Relational Databases
21.3 Relational Database Overview: Books Database
21.4 SQL
21.5 LINQ to SQL
21.6 LINQ to SQL: Extracting Information from a Database
21.7 More Complex LINQ Queries and Data Binding
21.8 Retrieving Data from Multiple Tables with LINQ
21.9 Creating a Master/Detail View Application
21.10 Programming with LINQ to SQL: Address-Book Case Study
21.11 Wrap-Up
21.12 Tools and Web Resources
22 ASP.NET 3.5 and ASP.NET AJAX 861
22.1 Introduction 862
22.2 Simple HTTP Transactions 863
22.3 Multitier Application Architecture 867
22.4 Creating and Running a Simple Web-Form Example 868
22.5 Web Controls 882
22.6 Session Tracking 899
22.7 Case Study: Connecting to a Database in ASP.NET 915
22.8 Case Study: Secure Books Database Application 924
22.9 ASP.NET AJAX 952
22.10 New ASP.NET 3.5 Data Controls 960
22.11 Wrap-Up 961
22.12 Web Resources 962
23 Windows Communication Foundation (WCF) Web Services 963
23.1 Introduction 964
23.2 WCF Services Basics 965
23.3 Simple Object Access Protocol (SOAP) 965
23.4 Representational State Transfer (REST) 966
23.5 JavaScript Object Notation (JSON) 966
23.7 Publishing and Consuming REST-Based XML Web Services 976
23.8 Publishing and Consuming REST-Based JSON Web Services 980
23.9 Blackjack Web Service: Using Session Tracking in a SOAP-Based Web Service 984
23.10 Airline Reservation Web Service: Database Access and Invoking a Service from ASP.NET 997
23.11 Equation Generator: Returning User-Defined Types 1002
23.12 Wrap-Up 1014
23.13 Deitel Web Services Resource Centers 1015
24 Silverlight, Rich Internet Applications and Multimedia 1016
24.1 Introduction 1017
24.2 Platform Overview 1018
24.3 Silverlight Runtime and Tools Installation 1019
24.4 Building a Silverlight WeatherViewer Application 1019
24.5 Animations and the FlickrViewer 1031
24.6 Images and Deep Zoom 1037
24.7 Audio and Video 1050
24.8 Isolated Storage 1055
24.9 Silverlight Demos and Web Resources 1056
24.10 Wrap-Up
1057
25 Data Structures 1059
25.1 Introduction 1060
25.2 Simple-Type structs, Boxing and Unboxing 1060
25.3 Self-Referential Classes 1061
25.4 Linked Lists 1062
25.5 Stacks 1075
25.6 Queues 1079
25.7 Trees 1082
25.8 Wrap-Up 1095
26 Generics 1097
27 Collections 1118
A Operator Precedence Chart 1145
B Simple Types 1147
C Number Systems 1149
D ATM Case Study Code 1159
E UML 2: Additional Diagram Types 1185
F ASCII Character Set 1187
G Unicode® 1188
H Using the Visual C# 2008 Debugger 1196
Index 1215