Table Of ContentTEAM LinG
01_597043 ffirs.qxd 9/20/05 1:05 PM Page i
C# 2005
FOR
DUMmIES
‰
by Stephen Randy Davis
and Chuck Sphar
01_597043 ffirs.qxd 9/20/05 1:05 PM Page ii
C# 2005 For Dummies®
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
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 permit-
ted 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.
Trademarks:Wiley, the Wiley Publishing 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, 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. Wiley Publishing, 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 REP-
RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON-
TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT
LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE-
ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON-
TAINED 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 FUR-
THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR-
MATION 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 800-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 also publishes its books in a variety of electronic formats. Some content that appears in print may
not be available in electronic books.
Library of Congress Control Number: 2005927620
ISBN-13: 978-0-7645-9704-6
ISBN-10: 0-7645-9704-3
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/RR/RQ/QV/IN
01_597043 ffirs.qxd 9/20/05 1:05 PM Page iii
About the Authors
Stephen R. Davis, who goes by the name of Randy, lives with his wife and son
near Dallas, Texas. He and his family have written numerous books, including
C++ For Dummiesand C++ Weekend Crash Course.Stephen works for L-3
Communications.
Chuck Spharescaped Microsoft’s C++ documentation camps in 1997, after six
years’ hard labor as a senior technical writer. He’s perpetrated two previous
tomes, one on object-oriented programming for the Mac and one on Microsoft’s
MFC class library. He’s currently finishing a novel about ancient Rome
(againstrome.com) and gobbling mouthfuls of .NET programming. Chuck
can be reached for praise and minor nits at [email protected].
01_597043 ffirs.qxd 9/20/05 1:05 PM Page iv
01_597043 ffirs.qxd 9/20/05 1:05 PM Page v
Dedication
For Pam and the Moms — Chuck Sphar
Acknowledgments
I would like to thank Claudette Moore and Debbie McKenna, who
brought the book to me. I also want to thank Randy Davis for being
willing to hand over his baby to a guy he didn’t know. I’d have
found that very hard, and I hope I’ve done justice in extending his
excellent first edition.
Many thanks are due as well to the fine folks at Wiley, starting with
Acquisitions Editor Katie Feltman and Project Editor Kim Darosett.
Kim’s astute shaping helped turn me into a For Dummiesauthor, no
mean feat. I’d also like to thank Chris Bower for his sharp technical
eye and excellent C# knowledge, John Edwards for much of the
book’s consistency, and the art, media, and other production folks
who turn my files into a real book.
The most heartfelt thanks are due to Pam for constant encour-
agement and much enabling. She’s my partner in all things.
— Chuck Sphar
01_597043 ffirs.qxd 9/20/05 1:05 PM Page vi
Publisher’s Acknowledgments
We’re proud of this book; please send us your comments through our online registration form
located at www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Composition Services
MediaDevelopment
Project Coordinator: Jennifer Theriot
Project Editor:Kim Darosett
Layout and Graphics: Carl Byers, Andrea Dahl,
Acquisitions Editor:Katie Feltman Joyce Haughey, StephanieD. Jumper,
Heather Ryan, Erin Zeltner
Copy Editor:John Edwards
Proofreaders: LeeannHarney,
Technical Editor:Chris Bower
Carl William Pierce, Dwight Ramsey,
Editorial Manager:Leah Cameron TECHBOOKS Production Services
Media Project Supervisor:Laura Moss Indexer: TECHBOOKS Production Services
Media Development Specialists:Angie Denny,
Travis Silvers, Kit Malone, Steve Kudirka
Media Development Manager:
LauraVanWinkle
Media Development Supervisor:
RichardGraves
Editorial Assistant:Amanda Foxworth
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
Diane Graves Steele,Vice President and Publisher
Joyce Pepple,Acquisitions Director
Composition Services
Gerry Fahey,Vice President of Production Services
Debbie Stailey,Director of Composition Services
02_597043 ftoc.qxd 9/20/05 1:05 PM Page vii
Contents at a Glance
Introduction ................................................................1
Part I: Creating Your First C# Programs ........................9
Chapter 1: Creating Your First C# Windows Program .................................................11
Chapter 2: Creating Your First C# Console Application .............................................29
Part II: Basic C# Programming ...................................37
Chapter 3: Living with Variability — Declaring Value-Type Variables ......................39
Chapter 4: Smooth Operators ........................................................................................57
Chapter 5: Controlling Program Flow ...........................................................................71
Part III: Object-Based Programming ...........................99
Chapter 6: Collecting Data — The Class and the Array ............................................101
Chapter 7: Putting on Some High-Class Functions ....................................................127
Chapter 8: Class Methods ............................................................................................163
Chapter 9: Stringing in the Key of C# ..........................................................................187
Part IV: Object-Oriented Programming ......................211
Chapter 10: Object-Oriented Programming — What’s It All About? .......................213
Chapter 11: Holding a Class Responsible ...................................................................221
Chapter 12: Inheritance — Is That All I Get? ..............................................................251
Chapter 13: Poly-what-ism? ..........................................................................................273
Part V: Beyond Basic Classes ...................................301
Chapter 14: When a Class Isn’t a Class — The Interface and the Structure ..........303
Chapter 15: Asking Your Pharmacist about Generics ...............................................333
Part VI: The Part of Tens ..........................................365
Chapter 16: The 10 Most Common Build Errors (And How to Fix Them) ..............367
Chapter 17: The 10 Most Significant Differences between C# and C++ ..................379
Appendix: About the CD ...........................................385
02_597043 ftoc.qxd 9/20/05 1:05 PM Page viii
Bonus Chapters on the CD-ROM!.................................CD
Bonus Chapter 1: Some Exceptional Exceptions ......................................................CD1
Bonus Chapter 2: Handling Files and Libraries in C# ............................................CD27
Bonus Chapter 3: Stepping through Collections ....................................................CD55
Bonus Chapter 4: Using the Visual Studio Interface ..............................................CD99
Bonus Chapter 5: C# on the Cheap ........................................................................CD139
Index.......................................................................391
End-User License Agreement........................Back of Book
02_597043 ftoc.qxd 9/20/05 1:05 PM Page ix
Table of Contents
Introduction .................................................................1
What’s New in C# 2.0 ......................................................................................2
About This Book ..............................................................................................3
What You Need to Use the Book ...................................................................3
How to Use This Book ....................................................................................4
How This Book Is Organized ..........................................................................4
Part I: Creating Your First C# Programs .............................................4
Part II: Basic C# Programming .............................................................4
Part III: Object-Based Programming ....................................................5
Part IV: Object-Oriented Programming ...............................................5
Part V: Beyond Basic Classes ...............................................................5
Part VI: The Part of Tens ......................................................................5
About the CD-ROM ................................................................................6
Icons Used in This Book .................................................................................6
Conventions Used in This Book ....................................................................7
Where to Go from Here ...................................................................................7
Part I: Creating Your First C# Programs .........................9
Chapter 1: Creating Your First C# Windows Program . . . . . . . . . . . . .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 2005? What about Visual C#? .......................14
Creating a Windows Application with C# ...................................................15
Creating the template .........................................................................15
Building and running your first Windows Forms program ............18
Painting pretty pictures ......................................................................20
Make it do something, Daddy ............................................................25
Trying out the final product ...............................................................27
Visual Basic 6.0 programmers, beware! ............................................28
Chapter 2: Creating Your First C# Console Application . . . . . . . . . . . .29
Creating a Console Application Template ..................................................29
Creating the source program .............................................................30
Taking it out for a test drive ...............................................................31
Creating Your First Real Console App ........................................................32
Reviewing the Console Application Template ...........................................33
The program framework .....................................................................33
Comments ............................................................................................34
The meat of the program ....................................................................34