Table Of ContentBeginning
Microsoft® Visual C#® 2008
Karli Watson
Christian Nagel
Jacob Hammer Pedersen
Jon D. Reid
Morgan Skinner
Eric White
Wiley Publishing, Inc.
ffffiirrss..iinndddd iiiiii 33//2244//0088 55::4455::2200 PPMM
ffffiirrss..iinndddd vviiiiii 33//2244//0088 55::4455::2211 PPMM
Beginning
Microsoft® Visual C#® 2008
Introduction .............................................................................................xxvii
Part I: The C# Language
Chapter 1: Introducing C# ..............................................................................3
Chapter 2: Writing a C# Program ................................................................. 13
Chapter 3: Variables and Expressions ........................................................... 31
Chapter 4: Flow Control ............................................................................... 59
Chapter 5: More About Variables .................................................................. 93
Chapter 6: Functions ................................................................................. 125
Chapter 7: Debugging and Error Handling ................................................... 155
Chapter 8: Introduction to Object-Oriented Programming............................. 185
Chapter 9: Defining Classes ....................................................................... 209
Chapter 10: Defining Class Members ......................................................... 241
Chapter 11: Collections, Comparisons, and Conversions .............................. 277
Chapter 12: Generics ................................................................................. 331
Chapter 13: Additional OOP Techniques...................................................... 371
Chapter 14: C# 3.0 Language Enhancements ............................................. 399
Part II: Windows Programming
Chapter 15: Basic Windows Programming .................................................. 433
Chapter 16: Advanced Windows Forms Features ......................................... 493
Chapter 17: Using Common Dialogs ........................................................... 537
Chapter 18: Deploying Windows Applications ............................................. 583
Part III: Web Programming
Chapter 19: Basic Web Programming ......................................................... 627
Chapter 20: Advanced Web Programming ................................................... 671
(Continued)
ffffiirrss..iinndddd ii 33//2244//0088 55::4455::1199 PPMM
Chapter 21: Web Services ......................................................................... 709
Chapter 22: Ajax Programming .................................................................. 739
Chapter 23: Deploying Web Applications .................................................... 759
Part IV: Data Access
Chapter 24: File System Data .................................................................... 777
Chapter 25: XML ....................................................................................... 819
Chapter 26: Introduction to LINQ ............................................................... 849
Chapter 27: LINQ to SQL ............................................................................ 895
Chapter 28: ADO.NET and LINQ over DataSet ............................................. 933
Chapter 29: LINQ to XML ........................................................................... 993
Part V: Additional Techniques
Chapter 30: Attributes ............................................................................. 1019
Chapter 31: XML Documentation ............................................................. 1049
Chapter 32: Networking ........................................................................... 1075
Chapter 33: Introduction to GDI+ ............................................................. 1107
Chapter 34: Windows Presentation Foundation ......................................... 1143
Chapter 35: Windows Communication Foundation ..................................... 1215
Chapter 36: Windows Workflow Foundation .............................................. 1247
Index ...................................................................................................... 1279
ffffiirrss..iinndddd iiii 33//2244//0088 55::4455::2200 PPMM
Beginning
Microsoft® Visual C#® 2008
Karli Watson
Christian Nagel
Jacob Hammer Pedersen
Jon D. Reid
Morgan Skinner
Eric White
Wiley Publishing, Inc.
ffffiirrss..iinndddd iiiiii 33//2244//0088 55::4455::2200 PPMM
Beginning Microsoft® Visual C#® 2008
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-19135-4
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data is available from the publisher.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted
under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing,
Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at
http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or
warranties with respect to the accuracy or completeness of the contents of this work and specifically
disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No
warranty may be created or extended by sales or promotional materials. The advice and strategies contained
herein may not be suitable for every situation. This work is sold with the understanding that the publisher is
not engaged in rendering legal, accounting, or other professional services. If professional assistance is
required, the services of a competent professional person should be sought. Neither the publisher nor the
author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in
this work as a citation and/or a potential source of further information does not mean that the author or the
publisher endorses the information the organization or Website may provide or recommendations it may
make. Further, readers should be aware that Internet Websites listed in this work may have changed or
disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department
within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related
trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the
United States and other countries, and may not be used without written permission. Microsoft and Visual
C# are registered trademarks of Microsoft Corporation in the United States and/or other countries. All other
trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any
product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not
be available in electronic books.
ffffiirrss..iinndddd iivv 33//2244//0088 55::4455::2200 PPMM
Thanks to everyone at Wiley for helping me through this project and reigning in my strange British stylings, to
assorted clients for giving me the time to write, and to Donna for keeping me sane and coping with my
temperamental back. Thanks also to friends and family for being patient with my deadline-laden lifestyle.
—Karli Watson
To my wife, Beth, and our children, Nathaniel, Timothy, and Walter. Thanks for your support and encouragement.
Also a big thank-you to my co-authors and the team at Wrox/Wiley.
—Jon Reid
To Mum and Dad: No words can express how much I love and miss you. Thanks for everything, you were brilliant.
—Morgan Skinner
ffffiirrss..iinndddd vv 33//2244//0088 55::4455::2200 PPMM
About the Authors
Karli Watson is a freelance IT specialist, author, and developer. He is also a technical consultant for
3form Ltd. (www.3form.net) and Boost.net (www.boost.net), and an associate technologist with
Content Master (www.contentmaster.com). For the most part, he immerses himself in .NET
(in particular, C#) and has written numerous books in the field. He specializes in communicating
complex ideas in a way that is accessible to anyone with a passion to learn, and spends much of his
time playing with new technology to find new things to teach people.
During those rare times when he isn’t doing the above, Karli is probably wishing he were hurtling down
a mountain on a snowboard or possibly trying to get his novel published. Either way, you’ll know him
by his brightly colored clothes.
Christian Nagel is a software architect, trainer, and consultant, and an associate of Thinktecture
(www.thinktecture.com), offering training and coaching based on Microsoft .NET technologies. His
achievements in the developer community have earned him a position as Microsoft Regional Director
and MVP for ASP.NET. He enjoys an excellent reputation as an author of several .NET books, such as
Professional C#, Pro .NET Network Programming, and Enterprise Services with the .NET Frameworks, and he
speaks regularly at international industry conferences.
Christian has more than 15 years of experience as a developer and software architect. He started his
computing career on PDP 11 and VAX/VMS, covering a variety of languages and platforms. Since 2000,
he has been working with .NET and C#, developing and architecting distributed solutions. He can be
reached at www.christiannagel.com.
Jacob Hammer Pedersen is a systems developer at Fujitsu Service, Denmark. He’s been programming the
PC since the early 1990s using various languages, including Pascal, Visual Basic, C/C++, and C#. Jacob
has co-authored a number of .NET books and works with a wide variety of Microsoft technologies,
ranging from SQL Server to Office extensibility. A Danish citizen, he works and lives in Aarhus, Denmark.
Jon D. Reid is the director of systems engineering at Indigo Biosystems, Inc. (www.indigobio.com),
an independent software vendor for the life sciences, where he develops in C# for the Microsoft
environment. He has co-authored many .NET books, including Beginning Visual C# 2005, Beginning C#
Databases: From Novice to Professional, Pro Visual Studio .NET, ADO.NET Programmer’s Reference, and
Professional SQL Server 2000 XML.
Morgan Skinner started programming at school in 1980 and has been hooked on computing ever since.
He now works for Microsoft as an application development consultant where he helps customers with
their architecture, design, coding, and testing. He’s been working with .NET since the PDC release in
2000, and has authored several MSDN articles and co-authored a couple of books on .NET. In his spare
time he relaxes by fighting weeds on his allotment. You can reach Morgan at www.morganskinner.com.
Eric White is an independent software consultant with more than 20 years of experience in building
management information systems and accounting systems. When he isn’t hunched over a screen
programming in C#, he is most likely to be found with an ice axe in hand, climbing some mountain.
ffffiirrss..iinndddd vvii 33//2244//0088 55::4455::2200 PPMM
Credits
Acquisitions Editor Vice President and Executive Publisher
Katie Mohr Joseph B. Wikert
Development Editor Project Coordinator, Cover
Maryann Steinhart Lynsey Stanford
Technical Editor Proofreaders
Douglas Parsons Jeremy Bagai,
Josh Chase,
Production Editor David Fine,
William A. Barton Reed Kellman,
Scott Klemp,
Copy Editor Josh Peterson,
Luann Rouff Todd Spencer,
Word One
Editorial Manager
Mary Beth Wakefield Indexer
Robert Swanson
Production Manager
Tim Tate
Vice President and Executive Group
Publisher
Richard Swadley
ffffiirrss..iinndddd vviiii 33//2244//0088 55::4455::2211 PPMM
Description:The book is aimed at novice programmers who wish to learn programming with C# and the .NET framework. The book starts with absolute programming basics. It then moves into Web and Windows programming, data access (databases and XML), and more advanced technologies such as graphics programming with GD