Microsoft Visual C# 2010
Step by Step
®
®
John Sharp
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2010 by John Sharp
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means
without the written permission of the publisher.
Library of Congress Control Number: 2009939912
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 QWT 5 4 3 2 1 0
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further information about
international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly
at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to
Microsoft, Microsoft Press, Excel, IntelliSense, Internet Explorer, Jscript, MS, MSDN, SQL Server, Visual Basic, Visual
C#, Visual C++, Visual Studio, Win32, Windows, and Windows Vista are either registered trademarks or trademarks of
the Microsoft group of companies. Other product and company names mentioned herein may be the trademarks of their
respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events
depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address,
logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without any
express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will
be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
Acquisitions Editor: Ben Ryan
Developmental Editor: Devon Musgrave
Project Editor: Rosemary Caperton
Editorial Production: Waypoint Press, www.waypointpress.com
Technical Reviewer: Per Blomqvist; Technical Review services provided by Content Master, a member of
CM Group, Ltd.
Cover: Tom Draper Design
Body Part No. X16-81630
Contents at a Glance
Part I
Introducing Microsoft Visual C# and Microsoft
Visual Studio 2010
Welcome to C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Working with Variables, Operators, and Expressions . . . . . . . . . . 27
Writing Methods and Applying Scope . . . . . . . . . . . . . . . . . . . . . . 47
Using Decision Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Using Compound Assignment and Iteration Statements . . . . . . 91
Managing Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 109
1
2
3
4
5
6
Part II
Understanding the C# Language
Creating and Managing Classes and Objects . . . . . . . . . . . . . . .
Understanding Values and References . . . . . . . . . . . . . . . . . . . . .
Creating Value Types with Enumerations and Structures . . . . .
Using Arrays and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Parameter Arrays . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Interfaces and Defining Abstract Classes . . . . . . . . . . .
Using Garbage Collection and Resource Management . . . . . . .
7
8
9
10
11
12
13
14
Part III Creating
15
16
17
18
19
20
21
129
151
173
191
219
231
253
279
Components
Implementing Properties to Access Fields . . . . . . . . . . . . . . . . . .
Using Indexers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interrupting Program Flow and Handling Events . . . . . . . . . . . .
Introducing Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enumerating Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Querying In-Memory Data by Using Query Expressions . . . . . .
Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
295
315
329
353
381
395
419
iii
iv
Contents at a Glance
Part IV Building
Windows Presentation Foundation
Applications
22 Introducing Windows Presentation Foundation . . . . . . . . . . . . . 443
23 Gathering User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
24 Performing Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Part V
Managing Data
25 Querying Information in a Database . . . . . . . . . . . . . . . . . . . . . . 535
26 Displaying and Editing Data by Using the Entity
Framework and Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Part VI Building
Professional Solutions with
Visual Studio 2010
27 Introducing the Task Parallel Library . . . . . . . . . . . . . . . . . . . . . . 599
28 Performing Parallel Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . 649
29 Creating and Using a Web Service . . . . . . . . . . . . . . . . . . . . . . . . 683
Appendix
Interoperating with Dynamic Languages . . . . . . . . . . . . . . . . . . . 717
About the Author
John Sharp is a principal technologist at Content Master,
part of CM Group Ltd, a technical authoring and consulting company. An expert on developing applications
by using the Microsoft .NET Framework and other technologies, John has produced numerous tutorials, white
papers, and presentations on distributed systems, SOA
and Web services, the C# language, and interoperability
issues. John has helped to develop a large number of
courses for Microsoft Training (he co-wrote the first C#
programming course for them) and he is also the author
of several popular books, including Microsoft Windows
Communication Foundation Step by Step.
What do
you think of
this book?
We want to hear from you!
To participate in a brief online survey, please visit:
microsoft.com/learning/booksurvey
Tell us how well this book meets your needs—what works effectively, and what we can
do better. Your feedback will help us continually improve our books and learning
resources for you.
Thank you in advance for your input!
Stay in touch!
To subscribe to the Microsoft Press® Book Connection Newsletter—for news on upcoming
books, events, and special offers—please visit:
microsoft.com/learning/books/newsletter
SurvPage_corp.indd 1
8/14/09 4:40 AM