Table Of ContentC# 5.0
ALL-IN-ONE
FOR
DUMmIES‰
by Bill Sempf, Chuck Sphar,
and Stephen Randy Davis
C# 5.0 All-in-One For Dummies®
Published by
John Wiley & Sons, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Hoboken, New Jersey
Published by John Wiley & Sons, Inc., Hoboken, New Jersey
Published simultaneously in Canada
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 Permissions Department, John
Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at
http://www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!,
The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, 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. All other
trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any
product or vendor mentioned in this book.
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 U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
For technical support, please visit www.wiley.com/techsupport.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material
included with standard print versions of this book may not be included in e-books or in print-on-demand.
If this book refers to media such as a CD or DVD that is not included in the version you purchased, you
may download this material at http://booksupport.wiley.com. For more information about Wiley
products, visit www.wiley.com.
Library of Congress Control Number: 2012955833
ISBN 978-1-118-38536-4 (pbk); ISBN 978-1-118-46205-8 (ePub); ISBN 978-1-118-60323-9 (eMobi);
ISBN 978-1-118-60344-4 (ePDF)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
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 defines 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 Windows 8 Programming with HTML5 For Dummies and
Visual Basic 2008 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 Certified
Professional, a Certified Internet Business Strategist, and a Certified Internet
Webmaster. My company is Products Of Innovative New Technology (usually
called POINT), and you can reach me at [email protected].
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 [email protected]. His C# material web
page (references throughout the book) is http:://csharp102.info.
Stephen R. Davis, who goes by the name Randy, lives with his wife and son
near Dallas, Texas.
Dedication
This edition is dedicated to the great folks at the For Dummies team at John
Wiley & Sons. They genuinely care about creating awesome books for the
developer community that are both technically sound and mildly humorous.
Can’t beat that combination.
Also, for Gabrielle, Adam, and Kaelan, who had to put up with another six
months of Daddy hiding in the basement.
Author’s 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
material that is the backbone of this book. I learned much just editing the
first two minibooks for use in this All-in-One. Also, thanks to Kelly Ewing and
Katie Feltman 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.
Chuck Sphar, aside from being one of the original authors of the book, also
tech edited this edition. He did a truly awesome job and discovered many
a hidden gotcha. Thanks to him for his effort. As always, thanks to my wife,
Gabrielle, for her support. Sometimes I just can’t believe how lucky I am.
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 and Editorial Composition Services
Project Editor: Kelly Ewing Project Coordinator: Sheree Montgomery
(Previous Edition: Christopher Morris) Layout and Graphics: Jennifer Creasey
Senior Acquisitions Editor: Katie Feltman Proofreader: BIM Indexing & Proofreading
Services
Technical Editor: Chuck Sphar
Indexer: BIM Indexing & Proofreading Services
Editorial Manager: Jodi Jensen
Editorial Assistant: Annie Sullivan
Sr. Editorial Assistant: Cherie Case
Cartoons: Rich Tennant (www.the5thwave.com)
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
Kathleen Nebenhaus, Vice President and Executive Publisher
Composition Services
Debbie Stailey, Director of Composition Services
Contents at a Glance
Introduction ................................................................ 1
Book I: The 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 ........................................................................................71
Chapter 5: Getting into the Program Flow ....................................................................83
Chapter 6: Lining Up Your Ducks with Collections ...................................................107
Chapter 7: Stepping through Collections....................................................................133
Chapter 8: Buying Generic ............................................................................................159
Chapter 9: Some Exceptional Exceptions ...................................................................181
Book II: Object-Oriented C# Programming ................. 201
Chapter 1: Object-Oriented Programming — What’s It All About? .........................203
Chapter 2: Showing Some Class ...................................................................................211
Chapter 3: We Have Our Methods ...............................................................................223
Chapter 4: Let Me Say This about this ........................................................................243
Chapter 5: Holding a Class Responsible .....................................................................257
Chapter 6: Inheritance: Is That All I Get? ....................................................................281
Chapter 7: Poly-what-ism? ............................................................................................303
Chapter 8: Interfacing with the Interface ....................................................................329
Chapter 9: Delegating Those Important Events .........................................................353
Chapter 10: Can I Use Your Namespace in the Library? ...........................................373
Chapter 11: Improving Productivity with Named and Optional Parameters .........395
Book III: Designing for C# ........................................ 403
Chapter 1: Writing Secure Code ...................................................................................405
Chapter 2: Accessing Data ............................................................................................417
Chapter 3: Fishing the FileStream ................................................................................437
Chapter 4: Accessing the Internet ...............................................................................457
Chapter 5: Creating Images...........................................................................................471
Chapter 6: Programming Dynamically! .......................................................................481
Chapter 7: Helping Out with Interop ...........................................................................493
Book IV: A Tour of Visual Studio ............................... 499
Chapter 1: Getting Started with Visual Studio ...........................................................501
Chapter 2: Using the Interface ......................................................................................517
Chapter 3: Customizing Visual Studio .........................................................................541
Chapter 4: Transforming Text Templates...................................................................557
Book V: Windows Development with WPF .................. 565
Chapter 1: Introducing WPF .........................................................................................567
Chapter 2: Understanding the Basics of WPF.............................................................577
Chapter 3: Data Binding in WPF ...................................................................................601
Chapter 4: Practical WPF ..............................................................................................623
Book VI: Web Development with ASP.NET.................. 649
Chapter 1: Looking at How ASP.NET Works with C# .................................................651
Chapter 2: Building Web Applications ........................................................................663
Chapter 3: Controlling Your Development Experience .............................................677
Chapter 4: Leveraging the .NET Framework ...............................................................705
Chapter 5: Digging into Web Construction .................................................................723
Book VII: Service-Oriented Development .................... 737
Chapter 1: Getting Acquainted with Web Services ....................................................739
Chapter 2: Building Web Services with ASMX ............................................................751
Chapter 3: Building Web Services with WCF ..............................................................765
Chapter 4: Building Web Services with REST .............................................................779
Index ...................................................................... 787
Description:C# is a complex programming language for building .NET-connected software for Microsoft Windows, cloud computing, the Web, and a wide range of devices. Todays developers use it to develop Azure and Windows Phone apps, and Android applications using Mono. Novice programmers can get up and running qui