|
C# 2008 For Dummies |
5 |
|
|
About the Authors |
7 |
|
|
Dedications |
9 |
|
|
Authors’ Acknowledgments |
9 |
|
|
Contents at a Glance |
11 |
|
|
Table of Contents |
13 |
|
|
Introduction |
23 |
|
|
What’s New in C# 3.0 |
24 |
|
|
About This Book |
25 |
|
|
What You Need to Use the Book |
25 |
|
|
How to Use This Book |
26 |
|
|
How This Book Is Organized |
26 |
|
|
Icons Used in This Book |
28 |
|
|
Conventions Used in This Book |
29 |
|
|
Where’s the Code? And the Bonus Goodies? |
29 |
|
|
Where to Go from Here |
30 |
|
|
Part I: Getting Started with C# |
31 |
|
|
Chapter 1: Creating Your First C# Console Application |
33 |
|
|
Getting a Handle on Computer Languages, C#, and .NET |
33 |
|
|
Creating Your First Console Application |
37 |
|
|
Making Your Console App Do Something |
41 |
|
|
Reviewing Your Console Application |
42 |
|
|
Introducing the Toolbox Trick |
44 |
|
|
Part II: Basic C# Programming |
47 |
|
|
Chapter 2: Living with Variability — Declaring Value-Type Variables |
49 |
|
|
Declaring a Variable |
49 |
|
|
What’s an int? |
50 |
|
|
Representing Fractions |
53 |
|
|
Handling Floating-Point Variables |
54 |
|
|
Using the Decimal Type — Is It an Integer or a Float? |
57 |
|
|
Examining the bool Type — Is It Logical? |
59 |
|
|
Checking Out Character Types |
59 |
|
|
What’s a Value-Type? |
62 |
|
|
Comparing string and char |
62 |
|
|
Is This a Leap Year? — DateTime |
63 |
|
|
Declaring Numeric Constants |
65 |
|
|
Changing Types — The Cast |
66 |
|
|
New Feature: Letting the C# Compiler Infer Data Types |
68 |
|
|
Chapter 3: Smooth Operators |
71 |
|
|
Performing Arithmetic |
71 |
|
|
Performing Logical Comparisons — Is That Logical? |
75 |
|
|
Matching Expression Types at TrackDownAMate. com |
79 |
|
|
Chapter 4: Getting into the Program Flow |
83 |
|
|
Branching Out with if and switch |
84 |
|
|
Here We Go Loop the Loop |
94 |
|
|
Looping a Specified Number of Times with for |
104 |
|
|
Nesting Loops |
107 |
|
|
Don’t goto Pieces |
108 |
|
|
Chapter 5: Lining Up Your Ducks with Collections |
111 |
|
|
The C# Array |
112 |
|
|
A Loop Made foreach Array |
118 |
|
|
Sorting Through Arrays of Data |
119 |
|
|
New Feature — Using var for Arrays |
124 |
|
|
Loosening Up with C# Collections |
125 |
|
|
Understanding Collection Syntax |
126 |
|
|
Using Lists |
127 |
|
|
Using Dictionaries |
130 |
|
|
New Feature — Array and Collection Initializers |
132 |
|
|
New Feature — Using Sets |
134 |
|
|
On Not Using Old-Fashioned Collections |
138 |
|
|
Chapter 6: Pulling Strings |
139 |
|
|
The Union Is Indivisible, and So Are Strings |
140 |
|
|
Performing Common Operations on a String |
141 |
|
|
Comparing Strings |
142 |
|
|
What If I Want to Switch Case? |
147 |
|
|
Looping Through a String |
148 |
|
|
Searching Strings |
149 |
|
|
Getting Input from the Command Line |
150 |
|
|
Controlling Output Manually |
156 |
|
|
Formatting Your Strings Precisely |
161 |
|
|
StringBuilder: Manipulating Strings More Efficiently |
166 |
|
|
Part III: Using Objects |
169 |
|
|
Chapter 7: Showing Some Class |
171 |
|
|
What’s an Object? What’s a Class? |
172 |
|
|
Accessing the Members of an Object |
174 |
|
|
An Example Object-Based Program |
175 |
|
|
Discriminating Between Objects |
176 |
|
|
Can You Give Me References? |
177 |
|
|
Classes That Contain Classes Are the Happiest Classes in the World |
179 |
|
|
Generating Static in Class Members |
180 |
|
|
Defining const and readonly Data Members |
182 |
|
|
Chapter 8: We Have Our Methods |
183 |
|
|
Defining and Using a Method |
183 |
|
|
An Example Method for Your Files |
185 |
|
|
Having Arguments with Methods |
191 |
|
|
Passing Value-Type and Reference-Type Arguments |
199 |
|
|
Returning Values after Christmas |
204 |
|
|
Chapter 9: Let Me Say This about this |
213 |
|
|
Passing an Object to a Method |
213 |
|
|
Defining Methods |
215 |
|
|
Accessing the Current Object |
220 |
|
|
New Feature: Extension Methods |
227 |
|
|
Chapter 10: Object-Oriented Programming — What’s It All About? |
231 |
|
|
Object-Oriented Concept # 1 — Abstraction |
231 |
|
|
Object-Oriented Concept # 2 — Classification |
233 |
|
|
Why Classify? |
234 |
|
|
Object-Oriented Concept # 3 — Usable Interfaces |
235 |
|
|
Object-Oriented Concept # 4 — Access Control |
236 |
|
|
How Does C# Support Object-Oriented Concepts? |
237 |
|
|
Part IV: Object-Oriented Programming |
239 |
|
|
Chapter 11: Holding a Class Responsible |
241 |
|
|
Restricting Access to Class Members |
241 |
|
|
Why Worry about Access Control? |
245 |
|
|
Defining Class Properties |
250 |
|
|
Getting Your Objects Off to a Good Start — Constructors |
254 |
|
|
The C#-Provided Constructor |
254 |
|
|
Replacing the Default Constructor |
256 |
|
|
Overloading the Constructor (Is That Like Overtaxing a Carpenter?) |
265 |
|
|
Avoiding Duplication among Constructors |
268 |
|
|
Being Object-Stingy |
271 |
|
|
Chapter 12: Inheritance — Is That All I Get? |
273 |
|
|
Inheriting a Class |
274 |
|
|
Why Do You Need Inheritance? |
276 |
|
|
A More Involved Example — Inheriting from a BankAccount Class |
276 |
|
|
IS_ A versus HAS_ A — I’m So Confused |
279 |
|
|
When to IS_ A and When to HAS_ A? |
283 |
|
|
Other Features That Support Inheritance |
283 |
|
|
The object Class |
287 |
|
|
Inheritance and the Constructor |
288 |
|
|
The Updated BankAccount Class |
293 |
|
|
Chapter 13: Poly-what-ism? |
297 |
|
|
Overloading an Inherited Method |
298 |
|
|
Polymorphism |
306 |
|
|
The Class Business Card: ToString() |
313 |
|
|
C# During Its Abstract Period |
313 |
|
|
Sealing a Class |
322 |
|
|
Chapter 14: Interfacing with the Interface |
325 |
|
|
What Is CAN_ BE_ USED_ AS? |
325 |
|
|
What Is an Interface? |
327 |
|
|
Using an Interface |
330 |
|
|
Using C#’s Predefined Interface Types |
332 |
|
|
Can I See a Program That CAN_ BE_ USED_ AS an Example? |
332 |
|
|
Unifying Class Hierarchies |
339 |
|
|
Hiding Behind an Interface |
341 |
|
|
Inheriting an Interface |
344 |
|
|
Using Interfaces to Manage Change in Object-Oriented Programs |
345 |
|
|
Part V: Now Showing in C# 3.0 |
353 |
|
|
Chapter 15: Delegating Those Important Events |
355 |
|
|
E. T. Phone Home — The Callback Problem |
355 |
|
|
What’s a Delegate? |
356 |
|
|
Pass Me the Code, Please — Examples |
358 |
|
|
How About a More Real-World Example? |
361 |
|
|
Shh! Keep It Quiet — Anonymous Methods |
367 |
|
|
Stuff Happens — C# Events |
368 |
|
|
Chapter 16: Mary Had a Little Lambda Expression |
375 |
|
|
Collections and the Do-to-Each Problem |
375 |
|
|
New Feature: Using Lambda Expressions |
376 |
|
|
Using Lambda Expressions for Any Delegate |
387 |
|
|
Chapter 17: LINQing Up with Query Expressions |
389 |
|
|
Reviewing the C# 3.0 Features |
390 |
|
|
What’s a Query, and Why Should I Care? |
390 |
|
|
Querying in C# 3.0 |
394 |
|
|
What Can I Do with a Query? |
398 |
|
|
Filtering Out the Espresso Grounds |
399 |
|
|
Selecting and Grouping What You Want |
401 |
|
|
Sorting the Stuff You Select |
411 |
|
|
Is There Any Method to Querying? |
413 |
|
|
Counting, Summing, Averaging, and Stuff Like That |
419 |
|
|
Treating an Object Like a Collection |
423 |
|
|
Part VI: The Part of Tens |
425 |
|
|
Chapter 18: Ten Common Build Errors (And How to Fix Them) |
427 |
|
|
Index |
439 |
|