Tải bản đầy đủ - 0 (trang)
Programming C# 3.0, Fifth Edition

Programming C# 3.0, Fifth Edition

Tải bản đầy đủ - 0trang

www.it-ebooks.info



FIFTH EDITION



Programming C# 3.0



Jesse Liberty and Donald Xie



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



www.it-ebooks.info



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]



www.it-ebooks.info



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



www.it-ebooks.info



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

Queues

Stacks

Dictionaries



iv |



Table of Contents



156

162

177

186

190

195

206

208

211



www.it-ebooks.info



10. Strings and Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

Strings

Regular Expressions



215

229



11. Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

Throwing and Catching Exceptions

Exception Objects



242

252



12. Delegates and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

Events

Events and Delegates

Anonymous Methods



256

257

271



Part II. C# and Data

13. Introducing LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

Defining and Executing a Query

LINQ and C#

Anonymous Types

Implicitly Typed Local Variables

Extension Methods

Lambda Expressions in LINQ



280

285

291

291

292

297



14. Working with XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

XML Basics (A Quick Review)

X Stands for eXtensible

Creating XML Documents

Searching in XML with XPath

Searching Using XPathNavigator

XML Serialization



302

304

304

311

322

329



15. Putting LINQ to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

Getting Set Up

LINQ to SQL Fundamentals

Using Visual Studio LINQ to SQL Designer

Retrieving Data

Updating Data Using LINQ to SQL

Deleting Relational Data

LINQ to XML



338

339

344

349

353

358

363



Table of Contents |



v



www.it-ebooks.info



16. ADO.NET and Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368

Relational Databases and SQL

The ADO.NET Object Model

Getting Started with ADO.NET



368

372

374



Part III. Programming with C#

17. Programming ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

Web Forms Fundamentals

Creating a Web Form

Data Binding



381

385

391



18. Programming WPF Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404

WPF in a Very Small Nutshell

Building the Application

What Have You Learned, Dorothy?



404

406

419



19. Programming Windows Forms Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 420

Creating the Application



420



Part IV. The CLR and the .NET Framework

20. Attributes and Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

Attributes

Reflection



449

456



21. Threads and Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465

Threads

Synchronization

Race Conditions and Deadlocks



466

474

485



22. Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487

Files and Directories

Reading and Writing Data

Asynchronous I/O

Network I/O

Web Streams

Serialization

Isolated Storage

vi |



Table of Contents



488

499

506

511

527

529

538



www.it-ebooks.info



23. Programming .NET and COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542

Importing ActiveX Controls

P/Invoke

Pointers



542

551

554



C# Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569



Table of Contents |



vii



www.it-ebooks.info



www.it-ebooks.info



Preface



1



In 2000, .NET revolutionized the way we create both web and Windows applications. .NET 2.0 was a dramatic incremental improvement over .NET 1.0. This book

covers C# 3.0 and .NET 3.5, and this time we are looking at an even more significant

set of changes.

C# 3.0 introduces a new generation of changes to a framework that takes an enormous leap forward, revolutionizing the way we program Windows applications, web

services, and, to a lesser degree, web applications.

In 2000, I wrote in the first edition of this book that Microsoft had “bet the company” on .NET. It was a good bet. In 2007, I bet my career on .NET by joining

Microsoft as senior program manager in the Silverlight Development Division.

Because one way (my preferred way) to program Silverlight is with C#, I have the

opportunity to stay very current with this mature yet rapidly evolving language. It is

an exciting time for C#; version 3.0 adds a number of tremendously useful features,

and the newest edition of Visual Studio makes programming with these features easier than ever.

It is my goal that you’ll find Programming C# 3.0 to be of great use whether this is

your first exposure to .NET programming, or you’ve been at it for some time. I’ll

start with the fundamentals, and introduce new additions to the language not as

obscure add-ons, but as the integrated features that they are.

If you are already a C# 2.0 programmer, feel free to skim through the parts you

know. The new features are called out by appropriate headings; you won’t inadvertently skip over them. But be sure to reread Chapter 12, and all of Parts II and III.



ix



www.it-ebooks.info



C# and .NET

The programming language of choice for .NET is C#, which builds on the lessons

learned from C (high performance), C++ (object-oriented structure), Java™ (garbage collection, high security), and Visual Basic (rapid development) to create a

language ideally suited for developing component-based, n-tier, distributed Windows

client and web applications.

C# 3.0 brings greatly enhanced features and a powerful new development environment. It is the crowning achievement of Microsoft’s R&D investment. It is wicked

cool.



About This Book

This book is a tutorial, both on C# and on writing .NET applications with C#.

If you are a proficient C# 2.0 programmer, and all you want to know is what is new

in C# 3.0, put this book down, buy Programming .NET 3.5 by myself and Alex

Horovitz (O’Reilly), and then read a lot about Language-Integrated Query (LINQ).

You’ll get by.

If, on the other hand, you want to brush up on your C# skills, or you are proficient

in another programming language such as C++ or Java, or even if C# is your first

programming language, this book is for you.

Note that for this edition I have been joined by a second author: Donald Xie. Donald

and I have worked together on a number of books for the past decade. He is smart,

diligent, and careful, and much of the work of this book is his, but every word in this

book is mine. Donald wrote and rewrote much of the new material, but he did so

knowing that I would then rewrite it so that this book speaks with a single voice. I

think it is imperative for a tutorial such as this to speak from the mind of a single

developer (me) into the mind of another developer (you) with as little distortion as

possible.



What You Need to Use This Book

To make the best use of this book, please obtain the latest release of Visual Studio

2008. Any edition will do, including the Express edition for C#.

For Chapter 16, you will want to ensure that SQL Server or SQL Server Express is

installed (it is normally installed automatically with Visual Studio), and you’ll want

to install the (old) Northwind database that was created for SQL Server 2000, but

which works fine with the latest SQL Server editions.

To run the Windows Presentation Foundation (WPF) example in Chapter 18, you’ll

need to be running Vista, or you’ll need to download the .NET 3.5 runtime.



x



|



Preface



www.it-ebooks.info



All of this is available on the Microsoft web site, at no cost. Go to http://www.

microsoft.com and type “C# Express” into the search window. The first or second

link should take you to the download page.

The source code for every example in this book is available through the O’Reilly site,

http://www.oreilly.com/catalog/9780596527433, or through my portal site: http://

www.jesseliberty.com. Please scroll to and click on the book site, then click on Books

and scroll to this book, and you should find a link to the source code.

In addition, I provide a private, free support forum for all my writing, which you can

also access through the portal.



How This Book Is Organized

Part I focuses on the details of the language, Part II examines how C# supports interacting with data, Part III discusses how to write .NET programs, and Part IV

describes how to use C# with the .NET Common Language Runtime (CLR) and

Framework Class Library (FCL).



Part I: The C# Language

Chapter 1, C# 3.0 and .NET 3.5

This chapter introduces you to the C# language and the .NET 3.5 platform.

Chapter 2, Getting Started: “Hello World”

This chapter demonstrates a simple program to provide a context for what

follows, and introduces you to the Visual Studio integrated development environment (IDE) and a number of C# language concepts.

Chapter 3, C# Language Fundamentals

This chapter presents the basics of the language, from built-in datatypes to

keywords.

Chapter 4, Classes and Objects

Classes define new types and allow programmers to extend the language so that

they can better model the problems they’re trying to solve. This chapter explains

the components that form the heart and soul of C#.

Chapter 5, Inheritance and Polymorphism

Classes can be complex representations and abstractions of things in the real

world. This chapter discusses how classes relate and interact.

Chapter 6, Operator Overloading

This chapter teaches you how to add operators to your user-defined types.

Chapter 7, Structs

This chapter introduce structs, which are lightweight objects that are more

restricted than classes and that make fewer demands on the operating system

and on memory.



Preface |



xi



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Programming C# 3.0, Fifth Edition

Tải bản đầy đủ ngay(0 tr)

×