Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Programming C# 4.0 phần 1 pdf
Nội dung xem thử
Mô tả chi tiết
Programming C# 4.0
SIXTH EDITION
Programming C# 4.0
Ian Griffiths, Matthew Adams, and Jesse Liberty
Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo
Programming C# 4.0, Sixth Edition
by Ian Griffiths, Matthew Adams, and Jesse Liberty
Copyright © 2010 Ian Griffiths and Matthew Adams. 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 (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or [email protected].
Editors: Mike Hendrickson and Laurel Ruma
Production Editor: Adam Zaremba
Copyeditor: Audrey Doyle
Proofreader: Stacie Arellano
Indexer: Jay Marchand
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
July 2001: First Edition.
February 2002: Second Edition.
May 2003: Third Edition.
February 2005: Fourth Edition.
December 2007: Fifth Edition.
August 2010: Sixth Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Programming C# 4.0, the image of an African crowned crane, and related trade
dress are trademarks of O’Reilly Media, Inc.
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.
ISBN: 978-0-596-15983-2
[M]
1280338225
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
1. Introducing C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Why C#? Why .NET? 1
The .NET Framework Class Library 2
Language Style 3
Composability 4
Managed Code 5
Continuity and the Windows Ecosystem 6
C# 4.0, .NET 4, and Visual Studio 2010 7
Summary 9
2. Basic Programming Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Getting Started 11
Namespaces and Types 14
Projects and Solutions 19
Comments, Regions, and Readability 24
Bad Comments 26
XML Documentation Comments 26
Variables 28
Variable Types 28
Expressions and Statements 35
Assignment Statements 38
Increment and Decrement Operators 38
Flow Control with Selection Statements 39
if Statements 40
switch and case Statements 45
Iteration Statements 47
foreach Statements 48
for Statements 50
while and do Statements 52
v
Breaking Out of a Loop 53
Methods 55
Summary 58
3. Abstracting Ideas with Classes and Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Divide and Conquer 59
Abstracting Ideas with Methods 59
Abstracting Ideas with Objects and Classes 62
Defining Classes 64
Representing State with Properties 64
Protection Levels 66
Initializing with a Constructor 68
Fields: A Place to Put Data 72
Fields Can Be Fickle, but const Is Forever 75
Read-only Fields and Properties 76
Related Constants with enum 79
Value Types and Reference Types 82
Too Many Constructors, Mr. Mozart 88
Overloading 88
Overloaded Methods and Default Named Parameters 89
Object Initializers 92
Defining Methods 95
Declaring Static Methods 98
Static Fields and Properties 99
Static Constructors 101
Summary 102
4. Extensibility and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Association Through Composition and Aggregation 104
Inheritance and Polymorphism 106
Replacing Methods in Derived Classes 109
Hiding Base Members with new 109
Replacing Methods with virtual and override 112
Inheritance and Protection 114
Calling Base Class Methods 116
Thus Far and No Farther: sealed 118
Requiring Overrides with abstract 121
All Types Are Derived from Object 127
Boxing and Unboxing Value Types 127
C# Does Not Support Multiple Inheritance of Implementation 132
C# Supports Multiple Inheritance of Interface 132
Deriving Interfaces from Other Interfaces 135
Explicit Interface Implementation 136
vi | Table of Contents