1. Trang chủ
  2. » Công Nghệ Thông Tin

accelerated c# 2010 trey nash phần 1 pot

67 371 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 67
Dung lượng 7,28 MB

Nội dung

Nash Accelerated C# 2010 Companion eBook Available this print for content only—size & color not accurate   CYAN   MAGENTA   YELLOW   BLACK   PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Trey Nash, author of Accelerated C# 2008 Accelerated C# 2005 Accelerated VB 2005 (with Guy Fouche) Accelerated VB 2008 (with Guy Fouche) US $39.99 Shelve in: .NET User level: Intermediate–Advanced THE APRESS ROADMAP Accelerated C# 2010 Pro C# 2010 and the .NET 4.0 Platform Pro LINQ: Language Integrated Query in C# 2008 Beginning Android Pro Android Games Pro Android www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2537-9 9 781430 225379 5 39 9 9 Accelerated C# 2010 Dear Reader, In your hands is a guide to creating effective C# 4.0 code. This book focuses precisely on the C# language, showing you how to write programs that are robust, fault-toler- ant, and ready to be put into widely available libraries. I won’t burden your time with endless discussions of libraries: Accelerated C# 2010 instead presents you with a well organized, focused and easy to read text about C# and all of the tried and true idioms, patterns, and design principles accrued in the object oriented arena and during the lifetime of the .NET Framework. With many short examples, I will show you how common design patterns are used routinely in the .NET Framework and how you should employ them in your own designs. I cover with you all the newer features of the C# language in detail - including the dynamic type, co- and contra-variance, extension methods, lambda expres- sions, and Language Integrated Query (LINQ) among others. The new dynamic type facilitates interoperability possibilities that were previously cumbersome to implement. Extension methods, lambda expressions, and LINQ foster a functional programming model within a traditionally imperative programming language and you’ll find that they expand your horizons and open up new possibilities of solu- tions using functional programming techniques! Another area of particular interest for C# developers is how we can write excep- tion-safe and fault-tolerant code. The .NET Framework includes several facilities, including Critical Execution Regions, to help protect the state of your application in the event of an asynchronous exception. In this book, I explain how to use these facilities to your advantage. Have fun programming and remember, defining contract before implementa- tion, striving to be exception neutral, applying performance analysis and optimiza- tion, and being frugal with your resource usage are all keys to a successful product. Trey Nash 7.5 x 9.25 spine = 1.21875" 656 page count THE EXPERT’S VOICE ®  IN C# C# 2010 Trey Nash The fast way to the latest and greatest in C# programming and .NET 4.0 Accelerated Accelerated C# 2010 ■ ■ ■ Trey Nash Accelerated C# 2010 Copyright © 2010 by Trey Nash All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2537-9 ISBN-13 (electronic): 978-1-4302-2538-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: Damien Foggon Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editors: Katie Stence and Nancy Sixsmith Compositor: Bob Cooper Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please e-mail info@apress.com, or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. Dedicated to the memory of a special friend Thor January 1999 – October 2008 For Maite for the love and support v Contents at a Glance Contents vii About the Author xxi About the Technical Reviewer xxii Acknowledgments xxiii Preface xxiv ■Chapter 1: C# Preview 1 ■Chapter 2: C# and the CLR 10 ■Chapter 3: C# Syntax Overview 17 ■Chapter 4: Classes, Structs, and Objects 43 ■ Chapter 5: Interfaces and Contracts 137 ■Chapter 6: Overloading Operators 165 ■Chapter 7: Exception Handling and Exception Safety 181 ■Chapter 8: Working with Strings 215 ■Chapter 9: Arrays, Collection Types, and Iterators 243 ■Chapter 10: Delegates, Anonymous Functions, and Events 279 ■Chapter 11: Generics 307 ■Chapter 12: Threading in C# 361 ■Chapter 13: In Search of C# Canonical Forms 429 ■Chaper 14: Extension Methods 489 ■Chaper 15: Lambda Expressions 517 ■Chaper 16: LINQ: Language Integrated Query 543 vi ■Chaper 17: Dynamic Types 577 Index 609 ■ CONTENTS vii Contents Contents at a Glance v Contents vii About the Author xxi About the Technical Reviewer xxii Acknowledgments xxiii Preface xxiv ■Chapter 1: C# Preview 1 Differences Between C# and C++ 1 C# 1 C++ 2 CLR Garbage Collection 3 Example of a C# Program 3 Overview of Features Added in C# 2.0 5 Overview of Features Added in C# 3.0 6 Overview of New C# 4.0 Features 7 Summary 7 ■Chapter 2: C# and the CLR 10 The JIT Compiler in the CLR 10 Assemblies and the Assembly Loader 11 Minimizing the Working Set of the Application 12 Naming Assemblies 12 Loading Assemblies 13 Metadata 13 ■ CONTENTS viii Cross-Language Compatibility 15 Summary 15 ■Chapter 3: C# Syntax Overview 17 C# Is a Strongly Typed Language 17 Expressions 18 Statements and Expressions 20 Types and Variables 21 Value Types 23 Enumerations 24 Flags Enumerations 25 Reference Types 26 Default Variable Initialization 27 Implicitly Typed Local Variables 28 Type Conversion 30 Array Covariance 31 Boxing Conversion 31 as and is Operators 32 Generics 34 Namespaces 35 Defining Namespaces 36 Using Namespaces 37 Control Flow 39 if-else, while, do-while, and for 39 switch 39 foreach 40 break, continue, goto, return, and throw 41 Summary 41 [...]... 10 4 Static (Class) Constructors 10 6 Instance Constructor and Creation Ordering .10 9 Destroying Objects .11 3 Finalizers .11 3 Deterministic Destruction .11 5 Exception Handling .11 5 Disposable Objects .11 6 The IDisposable Interface 11 6 The using Keyword .11 8 Method Parameter... Parameter Types .11 9 Value Arguments 12 0 x ■ CONTENTS ref Arguments .12 0 out Parameters .12 2 param Arrays 12 3 Method Overloading 12 3 Optional Arguments 12 4 Named Arguments 12 5 Inheritance and Virtual Methods 12 8 Virtual and Abstract Methods 12 9 override and new Methods... 12 9 sealed Methods 13 1 A Final Few Words on C# Virtual Methods 13 2 Inheritance, Containment, and Delegation 13 2 Choosing Between Interface and Class Inheritance 13 2 Delegation and Composition vs Inheritance .13 4 Summary 13 6 ■Chapter 5: Interfaces and Contracts 13 7 Interfaces Define Types 13 8 Defining... 18 1 How the CLR Treats Exceptions .18 1 Mechanics of Handling Exceptions in C# .18 2 Throwing Exceptions 18 2 Changes with Unhandled Exceptions Starting with NET 2.0 18 2 Syntax Overview of the try, catch, and finally Statements 18 3 Rethrowing Exceptions and Translating Exceptions .18 6 Exceptions Thrown in finally Blocks .18 9 Exceptions... 503 Borrowing from Functional Programming 505 The Visitor Pattern 511 Summary 515 ■Chaper 15 : Lambda Expressions 517 Introduction to Lambda Expressions 517 Lambda Expressions and Closures . 518 Closures in C# 1. 0 5 21 Closures in C# 2.0 523 Lambda Statements 524 Expression Trees ... . 91 Boxing and Unboxing .94 When Boxing Occurs .98 Efficiency and Confusion .10 0 System.Object 10 1 Equality and What It Means 10 3 The IComparable Interface 10 3 Creating Objects 10 3 The new Keyword 10 3 Using new with Value Types 10 3 Using new with Class Types 10 3... Rollback Behavior 211 Summary 214 ■Chapter 8: Working with Strings 215 String Overview 215 String Literals . 216 Format Specifiers and Globalization 217 Object.ToString, IFormattable, and CultureInfo 218 Creating and Registering Custom CultureInfo Types 219 Format Strings 2 21 Console.WriteLine and String.Format... 16 5 Types and Formats of Overloaded Operators .16 5 Operators Shouldn’t Mutate Their Operands 16 7 Does Parameter Order Matter? 16 7 Overloading the Addition Operator .16 8 Operators That Can Be Overloaded 16 9 Comparison Operators 17 0 Conversion Operators 17 3 Boolean Operators 17 6 Summary 17 9 ■Chapter... Matching Rules .15 0 Explicit Interface Implementation with Value Types 15 4 Versioning Considerations .15 6 Contracts 15 7 xi ■ CONTENTS Contracts Implemented with Classes 15 7 Interface Contracts 15 9 Choosing Between Interfaces and Classes 16 0 Summary 16 4 ■Chapter 6: Overloading Operators 16 5 Just Because You... 305 ■Chapter 11 : Generics 307 Difference Between Generics and C++ Templates 308 Efficiency and Type Safety of Generics 309 Generic Type Definitions and Constructed Types 311 Generic Classes and Structs 311 Generic Interfaces . 314 Generic Methods 315 Generic Delegates . 317 Generic Type Conversion . Objects 11 3 Finalizers 11 3 Deterministic Destruction 11 5 Exception Handling 11 5 Disposable Objects 11 6 The IDisposable Interface 11 6 The using Keyword 11 8 Method Parameter Types 11 9 Value. programming and .NET 4.0 Accelerated Accelerated C# 2 010 ■ ■ ■ Trey Nash Accelerated C# 2 010 Copyright © 2 010 by Trey Nash All rights reserved. No part of. product. Trey Nash 7.5 x 9.25 spine = 1. 218 75" 656 page count THE EXPERT’S VOICE ®  IN C# C# 2 010 Trey Nash The fast way to the latest and greatest in C# programming and .NET 4.0 Accelerated Accelerated

Ngày đăng: 05/08/2014, 09:45