OReilly.Programming.C.Sharp.3.0.5th.Edition.Jan.2008.pdf

(4448 KB) Pobierz
15271836 UNPDF
15271836.002.png
FIFTH EDITION
Programming C# 3.0
Jesse Liberty and Donald Xie
Beijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo
15271836.003.png
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 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]
15271836.004.png
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ix
Part I.
The C# Language
1. C# 3.0 and .NET 3.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
The Evolution of C#
3
The C# Language
4
The .NET Platform
6
2. Getting Started: “Hello World”
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
Classes, Objects, and Types
7
Developing “Hello World”
14
Using the Visual Studio 2008 Debugger
18
3. C# Language Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
Types
21
Variables and Constants
25
Whitespace
33
Statements
33
Operators
49
Preprocessor Directives
59
4. Classes and Objects
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61
Defining Classes
62
Creating Objects
67
Using Static Members
75
Destroying Objects
79
Passing Parameters
83
iii
15271836.005.png
Overloading Methods and Constructors
89
Encapsulating Data with Properties
92
readonly Fields
96
5. Inheritance and Polymorphism
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
98
Specialization and Generalization
98
Inheritance
101
Polymorphism
102
Abstract Classes
109
The Root of All Types: Object
113
Nesting Classes
115
6. Operator Overloading
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
118
Using the operator Keyword
118
Supporting Other .NET Languages
119
Creating Useful Operators
120
Logical Pairs
120
The Equality Operator
120
Conversion Operators
121
Putting Operators to Work
121
7. Structs
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
127
Defining Structs
128
Creating Structs
129
8. Interfaces
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
132
Defining and Implementing an Interface
132
Overriding Interface Implementations
147
Explicit Interface Implementation
151
9. Arrays, Indexers, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
156
Arrays
156
The foreach Statement
162
Indexers
177
Collection Interfaces
186
Constraints
190
List<T>
195
Queues
206
Stacks
208
Dictionaries
211
iv
|
Table of Contents
15271836.001.png
Zgłoś jeśli naruszono regulamin