Programming C# 3.0, Fifth Editionby Jesse Liberty and Donald Xie Copyright © 2008 O’Reilly Media, Inc.. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA
Trang 2Programming C# 3.0
FIFTH EDITION
Jesse Liberty and Donald Xie
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
Trang 3Programming C# 3.0, Fifth Edition
by Jesse Liberty and Donald Xie
Copyright © 2008 O’Reilly Media, Inc All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions
are also available for most titles (safari.oreilly.com) For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor: John Osborn
Developmental Editor: Brian MacDonald
Production Editor: Sumita Mukherji
Copyeditor: Audrey Doyle
Proofreader: Sumita Mukherji
Indexer: Angela Howard
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Jessamyn Read
Printing History:
July 2001: First Edition.
February 2002: Second Edition.
May 2003: Third Edition.
February 2005: Fourth Edition.
December 2007: Fifth Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc Programming C# 3.0, the image of an African crowned crane, and related trade
dress are trademarks of O’Reilly Media, Inc.
Java ™ is a trademark of Sun Microsystems, Inc Microsoft, MSDN, the NET logo, Visual Basic, Visual C++, Visual Studio, and Windows are registered trademarks of Microsoft Corporation.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
This book uses RepKover ™ , a durable and flexible lay-flat binding.
ISBN-10: 0-596-52743-8
ISBN-13: 978-0-596-52743-3
[M]
Trang 4Table of Contents
Preface ix
Part I The C# Language
1 C# 3.0 and NET 3.5 3
2 Getting Started: “Hello World” 7
3 C# Language Fundamentals 21
4 Classes and Objects 61
Trang 5iv | Table of Contents
5 Inheritance and Polymorphism 98
6 Operator Overloading 118
7 Structs 127
8 Interfaces 132
9 Arrays, Indexers, and Collections 156
Trang 6About the Authors
Jesse Liberty, currently a senior program manager on the Silverlight Development
Team at Microsoft, is the author of Programming NET 3.5, Learning ASP.NET with
AJAX (both for O’Reilly), and many other books He is a recognized NET expert
whose experience includes working as a software architect at PBS and as a
distin-guished software engineer at AT&T He can be reached at http://www.JesseLiberty.com.
Donald Xie has been programming since Apple II was known as state of the art He
has written a lot of applications using different languages and technologies Since the late 90s, Donald has focused on developing enterprise-strength business applica-tions using Microsoft technologies—especially with NET—from the very first beta
Donald is a coauthor of several books, including Pro Visual Studio NET (Apress), and Fast Track ADO.NET and Data-Centric NET Programming with C# (both for
Peer Information, Inc.) He has also written books on C++ and Visual Basic Currently, Donald works as a business analyst for Chevron
Colophon
The animal on the cover of Programming C# 3.0, Fifth Edition, is an African
crowned crane This tall, skinny bird wanders the marshes and grasslands of West and
East Africa (the Western and Eastern African crowned cranes are known as Balearica
pavonia pavonia and Balearica regulorum gibbericeps, respectively).
Adult birds stand about three feet tall and weigh six to nine pounds Inside their long necks is a five-foot long windpipe—part of which is coiled inside their breastbone— giving voice to loud calls that can carry for miles They live for about 22 years, spending most of their waking hours looking for the various plants, small animals, and insects they like to eat (One crowned crane food-finding technique, perfected during the 38 to 54 million years these birds have existed, is to stamp their feet as they walk, flushing out tasty bugs.) They are the only type of crane to perch in trees, which they do at night when sleeping
Social and talkative, African crowned cranes group together in pairs or families, and the smaller groups band together in flocks of more than 100 birds Their elaborate mating dance has served as a model for some of the dances of local people
The cover image is an original engraving from the 19th century The cover font is Adobe ITC Garamond The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSans Mono Condensed