Sunteți pe pagina 1din 11

Learning C# 3.

Jesse Liberty and Brian MacDonald

O'REILLY®
Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
Table of Contents

Preface xv

1. C# and .NET Programming 1


Installing C# Express 2
C# 3.0 and .NET 3.5 3
The .NET Platform 4
The .NET Framework 4
The C# Language 5
Your First Program: Hello World 6
The Compiler 10
Examining Your First Program 11
The Integrated Development Environment 16
Summary 17
Test Your Knowledge: Quiz 18
Test Your Knowledge: Exercise 19

2. Visual Studio 2008 and C# Express 2008 20


Before You Read Further 22
The Start Page 22
Projects and Solutions 22
Project Types 24
Templates 25
Inside the Integrated Development Environment 26
Building and Running Applications 28
Menus 29
The File Menu 29
The Edit Menu 30
The View Menu 36
The Refactor Menu 41
The Project Menu 41
The Build Menu 41
The Debug Menu 41
The Data Menu 41
The Format Menu 42
The Tools Menu 42
The Window Menu 43
The Help Menu 44
Summary 44
Test Your Knowledge: Quiz 45
Test Your Knowledge: Exercises 45

3. C# Language Fundamentals 46
Statements 46
Types 47
Numeric Types 48
Nonnumeric Types: char and bool 49
Types and Compiler Errors 50
WriteLine() and Output 51
Variables and Assignment 52
Definite Assignment 54
Implicitly Typed Variables 55
Casting 56
Constants 58
Literal Constants 58
Symbolic Constants 58
Enumerations 60
S trings 63
Whitespace 63
Summary 64
Test Your Knowledge: Quiz 65
Test Your Knowledge: Exercises 66

vi I Table of Contents
4. Operators 68
Expressions 68
The Assignment Operator (=) 69
Mathematical Operators 69
Simple Arithmetic Operators (+, *, /) 70
The Modulus Operator (%) 71
Increment and Decrement Operators 72
The Calculate and Reassign Operators 72
Increment or Decrement by 1 73
The Prefix and Postfix Operators 73
Relational Operators 75
Logical Operators and Conditionals 77
The Conditional Operator 78
Operator Precedence 79
Summary 81
Test Your Knowledge: Quiz 82
Test Your Knowledge: Exercises 83

5. Branching 85
Unconditional Branching Statements 86
Conditional Branching Statements 88
if Statements 88
Single-Statement if Blocks 90
Short-Circuit Evaluation 92
if else Statements 94
Nested if Statements 95
switch Statements 98
Fall-Through and Jump-to Cases 101
Switch an string Statements 102
ReadLine( ) and Input 103
Iteration (Looping) Statements 104
Creating Loops with goto 105
The while Loop 106
The do ...while Loop 108
The for Loop 109
Summary 118
Test Your Knowledge: Quiz 119
Test Your Knowledge: Exercises 120

Takle nf Cnntentgvii
6. Object-Oriented Programming 121
Creating Models 123
Classes and Objects 123
Defining a Class 124
Class Relationships 125
The Three Pillars of Object-Oriented Programming 126
Encapsulation 126
Specialization 127
Polymorphism 128
Object-Oriented Analysis and Design 129
Summary 130
Test Your Knowledge: Quiz 131
Test Your Knowledge: Exercises 131

7. Classes and Objects 133


Defining Classes 134
Instantiating Objects 135
Creating a Box Class 137
Access Modifiers 138
Method Arguments 139
Return Types 141
Constructors 142
Initializers 144
Object Initializers 146
Anonymous Types 146
The this Keyword 147
Static and Instance Members 148
Invoking Static Methods 149
Using Static Fields 151
Finalizing Objects 154
Memory Allocation: The Stack Versus the Heap 155
Summary 161
Test Your Knowledge: Quiz 162
Test Your Knowledge: Exercises 163

8. Inside Methods 165


Overloading Methods 165

viii I Table of Contents


Encapsulating Data with Properties 168
The get Accessor 171
The set Accessor 172
Automatic Properties 173
Returning Multiple Values 173
Passing Value Types by Reference 175
out Parameters and Definite Assignment 177
Summary 178
Test Your Knowledge: Quiz 178
Test Your Knowledge: Exercises 179

9. Basic Debugging 180


Setting a Breakpoint 181
Using the Debug Menu to Set Your Breakpoint 183
Setting Conditions and Hit Counts 183
Examining Values: The Autos and Locals Windows 184
Setting Your Watch 188
The Call Stack 189
Stopping Debugging 190
Summary 191
Test Your Knowledge: Quiz 192
Test Your Knowledge: Exercises 193

10. Arrays 197


Using Arrays 197
Declaring Arrays 198
Understanding Default Values 199
Accessing Array Elements 199
Arrays and Loops 200
The foreach Statement 203
Initializing Array Elements 204
The params Keyword 204
Multidimensional Arrays 205
Rectangular Arrays 206
Jagged Arrays 210
Array Methods 213
Sorting Arrays 214

Table of Contents 1 ix
Summary 216
Test Your Knowledge: Quiz 217
Test Your Knowledge: Exercises 218

11. Inheritance and Polymorphism 219


Specialization and Generalization 219
Inheritance 222
Implementing Inheritance 222
Calling the Base Class Constructor 225
Hiding the Base Class Method 225
Controlling Access 226
Polymorphism 227
Creating Polymorphic Types 227
Overriding Virtual Methods 230
Using Objects Polymorphically 230
Versioning with new and override 232
Abstract Classes 234
Sealed Classes 237
The Root of All Classes: Object 237
Summary 240
Test Your Knowlege: Quiz 241
Test Your Knowledge: Exercises 241

12. Operator Overloading 243


Designing the Fraction Class 243
Using the operator Keyword 244
Creating Useful Operators 248
The Equals Operator 248
Conversion Operators 253
Summary 257
Test Your Knowledge: Quiz 258
Test Your Knowledge: Exercises 259

13. Interfaces 260


What Interfaces Are 260
Implementing an Interface 262
Defining the Interface 265
Implementing the Interface an the Client 266
Implementing More Than One Interface 267

x Table of Contents
Casting to an Interface 270
The is and as Operators 270
Extending Interfaces 276
Combining Interfaces 279
Overriding Interface Methods 280
Explicit Interface Implementation 285
Summary 288
Test Your Knowledge: Quiz 290
Test Your Knowledge: Exercises 290

14. Generics and Collections 292


Generics 292
Collection Interfaces 293
Creating Your Own Collections 293
Creating Indexers 293
Indexers and Assignment 298
Indexing an Other Values 298
Generic Collection Interfaces 302
The IEnumerable<T> Interface 303
Framework Generic Collections 307
Generic Lists: List<T> 307
Generic Queues 319
Generic Stacks 322
Dictionaries 325
Summary 328
Test Your Knowledge: Quiz 329
Test Your Knowledge: Exercises 330

15. Strings 331


Creating Strings 332
String Literals 332
Escape Characters 332
Verbatim Strings 333
The ToString( ) Method 333
Manipulating Strings 334
Comparing Strings 334
Concatenating Strings 336
Copying Strings 337

Table of Contents 1 xi
Testing for Equality 339
Other Useful String Methods 341
Finding Substrings 344
Splitting Strings 346
The StringBuilder Class 348
Regular Expressions 350
The Regex Class 351
Summary 353
Test Your Knowledge: Quiz 354
Test Your Knowledge: Exercises 355

16. Throwing and Catching Exceptions 357


Bugs, Errors, and Exceptions 358
Throwing Exceptions 358
Searching for an Exception Handler 358
The throw Statement 359
The try and catch Statements 361
How the Call Stack Works 364
Creating Dedicated catch Statements 366
The finally Statement 368
Exception Class Methods and Properties 370
Custom Exceptions 374
Summary 377
Test Your Knowledge: Quiz 378
Test Your Knowledge: Exercises 378

17. Delegates and Events 380


Delegates 381
Events 385
Publishing and Subscribing 386
Events and Delegates 387
Solving Delegate Problems with Events 394
The event Keyword 395
Using Anonymous Methods 399
Lambda Expressions 400
Summary 401
Test Your Knowledge: Quiz 402
Test Your Knowledge: Exercises 403

xii I Tobte of Contents


18. Creating Windows Applications 404
Creating a Simple Windows Form 404
Using the Visual Studio Designer 405
Creating a Real-World Application 411
Creating the Basic UI Form 412
Populating the TreeView Controls 415
Handling the TreeView Events 422
Handling the Button Events 426
Source Code 431
Summary 439
Test Your Knowledge: Quiz 440
Test Your Knowledge: Exercises 441

19. Windows Presentation Foundation 442


Your First WPF Application 443
WPF Differences from Windows Forms 447
Using Resources 450
Animations 452
Triggers and Storyboards 453
Animations As Resources 456
C# and WPF 460
Grids and Stack Panels 461
Adding Data 466
Using the Data in the XAML 468
Defining the ListBox 468
Event Handling 470
The Complete XAML File 471
Summary 474
Test Your Knowledge: Quiz 476
Test Your Knowledge: Exercises 476

20. ADO.NET and Relational Databases 477


Relational Databases and SQL 478
Installing the Northwind Database 478
Tables, Records, and Columns 481
Normalization 482
Declarative Referential Integrity 482
SQL 483

Table of Contents 1 xiii


The ADO.NET Object Model 485
DataTables and DataColumns 485
DataRelations 485
Rows 485
DataAdapter 486
DbCommand and DbConnection 486
DataReader 486
Getting Started with ADO.NET 486
Summary 489
Test Your Knowledge: Quiz 490
Test Your Knowledge: Exercises 491

21. LINQ 492


Querying In-Memory Data 492
Anonymous Types and Implicitly Typed Variables 497
Lambda Expressions 499
Ordering and Joining 500
Using LINQ with SQL 505
Using the Object Relational Designer 508
Summary 513
Test Your Knowledge: Quiz 514
Test Your Knowledge: Exercises 515

Appendix: Answers to Quizzes and Exercises 517

Index 649

xiv Table of Contents

S-ar putea să vă placă și