Tải bản đầy đủ (.pdf) (7 trang)

a0032 oreilly programming c sharp 3 0 5th edition jan 200 morebook vn 5318

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (388.23 KB, 7 trang )


FIFTH EDITION

Programming C# 3.0

Jesse Liberty and Donald Xie

Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo


Programming 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

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]


Table of Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix

Part I.

The C# Language

1. C# 3.0 and .NET 3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Evolution of C#
The C# Language
The .NET Platform

3
4
6

2. Getting Started: “Hello World” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Classes, Objects, and Types
Developing “Hello World”
Using the Visual Studio 2008 Debugger


7
14
18

3. C# Language Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Types
Variables and Constants
Whitespace
Statements
Operators
Preprocessor Directives

21
25
33
33
49
59

4. Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Defining Classes
Creating Objects
Using Static Members
Destroying Objects
Passing Parameters

62
67
75
79

83
iii


Overloading Methods and Constructors
Encapsulating Data with Properties
readonly Fields

89
92
96

5. Inheritance and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Specialization and Generalization
Inheritance
Polymorphism
Abstract Classes
The Root of All Types: Object
Nesting Classes

98
101
102
109
113
115

6. Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Using the operator Keyword
Supporting Other .NET Languages

Creating Useful Operators
Logical Pairs
The Equality Operator
Conversion Operators
Putting Operators to Work

118
119
120
120
120
121
121

7. Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Defining Structs
Creating Structs

128
129

8. Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Defining and Implementing an Interface
Overriding Interface Implementations
Explicit Interface Implementation

132
147
151


9. Arrays, Indexers, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Arrays
The foreach Statement
Indexers
Collection Interfaces
Constraints
List<T>
Queues
Stacks
Dictionaries

iv |

Table of Contents

156
162
177
186
190
195
206
208
211


About 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 distinguished software engineer at AT&T. He can be reached at .
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 applications 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.





×