About the Authors
Hi, my name is Bill Sempf, and I am a software architect. Although I used to
hate the term architect, it’s clearly the only thing out there that de nes what
I do. My breadth of experience includes business and technical analysis,
software design, development, testing, server management and maintenance,
and security. In my 17 years of professional experience, I have participated
in the creation of well over 200 applications for large and small companies,
managed the software infrastructure of two Internet service providers,
coded complex software happily in every environment imaginable, and made
mainframes talk to cellphones. In short, I make the technology products that
people are using every day play nicely together.
I started playing with computers in 1979 and haven’t looked back since. In
1985 I was helping my father (also named Bill) manage Apple IIe systems at
the local library. Since then I have built applications for the likes of Bank
One, Lucent Technologies, Nationwide Insurance, and Sears, Roebuck and
Co. I am the author of Visual Basic 2008 For Dummies and Visual Basic 2005
For Dummies; a coauthor of Effective Visual Studio.NET, Professional ASP.
NET Web Services, and Professional VB.NET; a frequent contributor to MSDN,
Builder.com, Hardcore Web Services, Cloud Computing Journal, Inside Web
Development Journal, and Intranet Journal; and have recently been an invited
speaker for the ACM and IEEE, DevEssentials, the International XML Web
Services Expo, and the Association of Information Technology Professionals.
I am a graduate of The Ohio State University with a bachelor’s of science
degree in business administration, a Microsoft Certi ed Professional, a
Certi ed Internet Business Strategist, and a Certi ed Internet Webmaster. My
company is Products Of Innovative New Technology (usually called POINT),
and you can reach me at bill@pointWeb.net.
Chuck Sphar escaped the Microsoft C++ documentation camps after six
years of hard labor as a senior technical writer. You can reach Chuck for
praise and minor nits at csharp@chucksphar.com. His C# material Web
page (references throughout the book) is csharp102.info.
Stephen R. Davis, who goes by the name Randy, lives with his wife and son
near Dallas, Texas.
01_563489-ffirs.indd iii01_563489-ffirs.indd iii 3/22/10 5:29 PM3/22/10 5:29 PM
01_563489-ffirs.indd iv01_563489-ffirs.indd iv 3/22/10 5:29 PM3/22/10 5:29 PM
Dedication
This book goes to the active community of Microsoft developers that I get
to work with every day. Thanks for your commitment to getting things done
right, sharing what you know, and having a good time doing it.
Also, for Gabrielle and Adam, who had to put up with another six months of
Daddy hiding in the basement.
—Sempf
Acknowledgments
A lot of people work to make a book of this size. Don’t think, just because the
authors are listed on the front page, that they conceived every idea in the
book. It takes a community.
First, thanks to Chuck Sphar and Randy Davis for the fantastic source mate-
rial that is the backbone of this book. I learned much just editing the rst two
minibooks for use in this All-in-One. Also, thanks to Katie Feltman and Chris
Morris for their editorial expertise.
A number of community members had a huge part in the creation of this book.
Carey Payette and Phil Japikse built Book V (about WPF) basically from the
goodness of their hearts, and I couldn’t have completed it without them — my
WPF skills aren’t worth writing about. These two get the award for Biggest
Contribution, and I thank them both.
The developers at Information Control Corporation were also essential in for-
mulating the initial scope of this book and then fact-checking the details. Steve
Webb, Stephen Gif n, John Hannah, Larry Beall, Michael Birchmeyer, and Azher
Muhammad all had a big part, especially in the information related speci cally to
C# 4.0. Thanks to them and all the other ICC experts who gave me ideas and tips.
Kevin Pilch-Bisson at Microsoft provided some C# clarity via Twitter through-
out the scope of this book. Steve Andrews provided the structure for the
T4 chapter in Book IV. Mads Torgerson reviewed the table of contents, and
I thank him for the “It looks delicious” phrase, which I think was my most
quoted phrase of the year.
Lars Corneliussen provided a few choice pointers for the book, and Julie
Lerman’s Entity Framework writing was the basis of my own additions to the
ADO.NET chapter.
As always, thanks to my wife, Gabrielle, for her support. Sometimes I just
can’t believe how lucky I am.
01_563489-ffirs.indd v01_563489-ffirs.indd v 3/22/10 5:29 PM3/22/10 5:29 PM
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments at http://dummies.custhelp.com.
For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974,
outside the U.S. at 317-572-3993, or fax 317-572-4002.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Media
Development
Senior Project Editor: Christopher Morris
Acquisitions Editor: Katie Feltman
Copy Editors: Debbye Butler, Heidi Unger,
Becky Whitney
Technical Editor: Mike Spivey
Editorial Manager: Kevin Kirschner
Media Development Project Manager:
Laura Moss-Hollister
Media Development Assistant Project
Manager: Jenny Swisher
Media Development Associate Producers:
Josh Frank, Marilyn Hummel,
Douglas Kuhn, Shawn Patrick
Editorial Assistant: Amanda Graham
Sr. Editorial Assistant: Cherie Case
Cartoons: Rich Tennant
(www.the5thwave.com)
Composition Services
Project Coordinator: Sheree Montgomery
Layout and Graphics: Samantha K. Cherolis,
Nikki Gately, Joyce Haughey
Proofreader: Christine Sabooni
Indexer: Broccoli Information Mgt.
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Composition Services
Debbie Stailey, Director of Composition Services
01_563489-ffirs.indd vi01_563489-ffirs.indd vi 3/22/10 5:29 PM3/22/10 5:29 PM
Contents at a Glance
Introduction 1
Book I: Basics of C# Programming 9
Chapter 1: Creating Your First C# Console Application 11
Chapter 2: Living with Variability — Declaring Value-Type Variables 25
Chapter 3: Pulling Strings 45
Chapter 4: Smooth Operators 73
Chapter 5: Getting Into the Program Flow 85
Chapter 6: Lining Up Your Ducks with Collections 109
Chapter 7: Stepping through Collections 135
Chapter 8: Buying Generic 169
Chapter 9: Some Exceptional Exceptions 187
Book II: Object-Oriented C# Programming 205
Chapter 1: Object-Oriented Programming: What’s It All About? 207
Chapter 2: Showing Some Class 215
Chapter 3: We Have Our Methods 227
Chapter 4: Let Me Say This about this 247
Chapter 5: Holding a Class Responsible 261
Chapter 6: Inheritance: Is That All I Get? 285
Chapter 7: Poly-what-ism? 307
Chapter 8: Interfacing with the Interface 333
Chapter 9: Delegating Those Important Events 357
Chapter 10: Can I Use Your Namespace in the Library? 377
Book III: Designing for C# 399
Chapter 1: Writing Secure Code 401
Chapter 2: Accessing Data 415
Chapter 3: Fishing the FileStream 435
Chapter 4: Accessing the Internet 455
Chapter 5: Creating Images 469
02_563489-ftoc.indd vii02_563489-ftoc.indd vii 3/19/10 8:01 PM3/19/10 8:01 PM
Book IV: A Tour of Visual Studio 479
Chapter 1: Getting Started with Visual Studio 481
Chapter 2: Using the Interface 495
Chapter 3: Customizing Visual Studio 517
Chapter 4: Transforming Text Templates 533
Book V: Windows Development with WPF 543
Chapter 1: Introducing WPF 545
Chapter 2: Understanding the Basics of WPF 555
Chapter 3: Data Binding in WPF 579
Chapter 4: Practical WPF 601
Book VI: Web Development with ASP.NET 627
Chapter 1: Looking at How ASP.NET Works with C# 629
Chapter 2: Building Web Applications 641
Chapter 3: Controlling Your Development Experience 659
Chapter 4: Leveraging the .NET Framework 685
Chapter 5: Digging into Web Construction 703
Book VII: Service-Oriented Development 717
Chapter 1: Getting Acquainted with Web Services 719
Chapter 2: Building Web Services with ASMX 731
Chapter 3: Building Web Services with WCF 745
Chapter 4: Building Web Services with ReST 759
Book VIII: New Features in C# 4.0 767
Chapter 1: Programming Dynamically! 769
Chapter 2: Improving Productivity with Named and Optional Parameters 781
Chapter 3: Helping Out with Interop 789
Chapter 4: Revising Generics 795
Index 799
02_563489-ftoc.indd viii02_563489-ftoc.indd viii 3/19/10 8:01 PM3/19/10 8:01 PM
Table of Contents
Introduction 1
What’s New in C# 4.0 2
About This Book 3
What You Need in Order to Use This Book 3
How to Use This Book 4
How This Book Is Organized 4
Book I: The Basics of C# Programming 5
Book II: Object-Oriented C# Programming 5
Book III: Designing for C# 5
Book IV: A Tour of Visual Studio 5
Book V: Windows Development with WPF 5
Book VI: Web Development with ASP.NET 6
Book VII: Service-Oriented Development 6
Book VIII: New Features in C# 4.0 6
Icons Used in This Book 6
Conventions Used in This Book 7
About this book’s Web site 7
Where to Go from Here 8
Book I: Basics of C# Programming 9
Chapter 1: Creating Your First C# Console Application. . . . . . . . . . . . .11
Getting a Handle on Computer Languages, C#, and .NET 11
What’s a program? 12
What’s C#? 12
What’s .NET? 13
What is Visual Studio 2010? What about Visual C#? 14
Creating Your First Console Application 14
Creating the source program 15
Taking it out for a test drive 18
Making Your Console App Do Something 19
Reviewing Your Console Application 20
The program framework 20
Comments 21
The meat of the program 21
Introducing the Toolbox Trick 22
Saving code in the Toolbox 23
Reusing code from the Toolbox 23
02_563489-ftoc.indd ix02_563489-ftoc.indd ix 3/19/10 8:01 PM3/19/10 8:01 PM
C# 2010 All-in-One For Dummies
x
Chapter 2: Living with Variability — Declaring
Value-Type Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Declaring a Variable 25
What’s an int? 26
Rules for declaring variables 27
Variations on a theme: Different types of int 27
Representing Fractions 28
Handling Floating-Point Variables 29
Declaring a oating-point variable 30
Converting some more temperatures 31
Examining some limitations of oating-point variables 31
Using the Decimal Type: Is It an Integer or a Float? 32
Declaring a decimal 33
Comparing decimals, integers, and oating-point types 33
Examining the bool Type: Is It Logical? 34
Checking Out Character Types 34
The char variable type 34
Special chars 35
The string type 35
What’s a Value Type? 36
Comparing string and char 37
Calculating Leap Years: DateTime 38
Declaring Numeric Constants 40
Changing Types: The Cast 41
Letting the C# Compiler Infer Data Types 42
Chapter 3: Pulling Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
The Union Is Indivisible, and So Are Strings 46
Performing Common Operations on a String 47
Comparing Strings 48
Equality for all strings: The Compare() method 48
Would you like your compares with or without case? 51
What If I Want to Switch Case? 52
Distinguishing between all-uppercase and all-lowercase strings 52
Converting a string to upper- or lowercase 52
Looping through a String 53
Searching Strings 54
Can I nd it? 54
Is my string empty? 55
Getting Input from the Command Line 55
Trimming excess white space 55
Parsing numeric input 56
Handling a series of numbers 58
Joining an array of strings into one string 60
Controlling Output Manually 60
Using the Trim() and Pad() methods 61
Using the Concatenate() method 63
Let’s Split() that concatenate program 64
02_563489-ftoc.indd x02_563489-ftoc.indd x 3/19/10 8:01 PM3/19/10 8:01 PM
Table of Contents
xi
Formatting Your Strings Precisely 65
StringBuilder: Manipulating Strings More Ef ciently 69
Chapter 4: Smooth Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
Performing Arithmetic 73
Simple operators 73
Operating orders 74
The assignment operator 75
The increment operator 76
Performing Logical Comparisons — Is That Logical? 77
Comparing oating-point numbers: Is your oat
bigger than mine? 78
Compounding the confusion with compound
logical operations 79
Matching Expression Types at TrackDownAMate.com 80
Calculating the type of an operation 81
Assigning types 82
Chapter 5: Getting Into the Program Flow . . . . . . . . . . . . . . . . . . . . . . . .85
Branching Out with if and switch 86
Introducing the if statement 86
Examining the else statement 89
Avoiding even the else 90
Nesting if statements 90
Running the switchboard 92
Here We Go Loop-the-Loop 95
Looping for a while 95
Doing the do . . . while loop 99
Breaking up is easy to do 99
Looping until you get it right 100
Focusing on scope rules 103
Looping a Speci ed Number of Times with for 104
An example 105
Why do you need another loop? 105
Nesting Loops 106
Don’t goto Pieces 107
Chapter 6: Lining Up Your Ducks with Collections . . . . . . . . . . . . . . .109
The C# Array 109
The argument for the array 110
The xed-value array 110
The variable-length array 112
The Length property 114
Initializing an array 115
A Loop Made foreach Array 115
Sorting Arrays of Data 116
New Feature: Using var for Arrays 120
Loosening Up with C# Collections 121
02_563489-ftoc.indd xi02_563489-ftoc.indd xi 3/19/10 8:01 PM3/19/10 8:01 PM
C# 2010 All-in-One For Dummies
xii
Understanding Collection Syntax 122
Figuring out <T> 123
Going generic 124
Using Lists 124
Using Dictionaries 126
Array and Collection Initializers 128
Initializing arrays 128
Initializing collections 129
Using Sets 130
On Not Using Old-Fashioned Collections 134
Chapter 7: Stepping through Collections. . . . . . . . . . . . . . . . . . . . . . . .135
Iterating through a Directory of Files 135
Iterating foreach Collections: Iterators 141
Accessing a collection: The general problem 141
Letting C# access data foreach container 143
Accessing Collections the Array Way: Indexers 145
Indexer format 145
An indexer program example 146
Looping Around the Iterator Block 150
Iterating days of the month: A rst example 154
What a collection is, really 155
Iterator syntax gives up so easily 156
Iterator blocks of all shapes and sizes 158
Where you can put your iterator 161
Chapter 8: Buying Generic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169
Writing a New Prescription: Generics 169
Generics are type-safe 170
Generics are ef cient 171
Classy Generics: Writing Your Own 171
Shipping packages at OOPs 172
Queuing at OOPs: PriorityQueue 172
Unwrapping the package 177
Touring Main() 178
Writing generic code the easy way 179
Saving PriorityQueue for last 180
Using a (nongeneric) Simple Factory class 182
Tending to un nished business 184
Chapter 9: Some Exceptional Exceptions . . . . . . . . . . . . . . . . . . . . . . .187
Using an Exceptional Error-Reporting Mechanism 187
About try blocks 189
About catch blocks 189
About nally blocks 190
What happens when an exception is thrown 190
Throwing Exceptions Yourself 192
02_563489-ftoc.indd xii02_563489-ftoc.indd xii 3/19/10 8:01 PM3/19/10 8:01 PM
Không có nhận xét nào:
Đăng nhận xét