Dear Reader, Offer Ends June 30, 2008 . - Kasetsart University

1y ago
12 Views
2 Downloads
6.29 MB
546 Pages
Last View : 19d ago
Last Download : 3m ago
Upload by : Camden Erdman
Transcription

CYANMAGENTAYELLOWBLACKPANTONE 123 CBooks for professionals by professionals Free CompanioneBook AvailableAccelerated C# 2008Accelerated C# 2005Coauthor ofAccelerated VB 2005Accelerated VB 2008In your hands is a guide to creating effective C# 3.0 code. This book focusesprecisely on the C# 3.0 language, showing you how to write programs that arerobust, fault-tolerant, and ready to be put into widely available libraries.Accelerated C# 2008 won’t burden your time with endless discussions oflibraries. Instead, it presents you with a well organized, focused and easy-toread text about C# 3.0 and all of the tried and true idioms, patterns, and designprinciples developed in the object oriented arena during the lifetime of the.NET Framework. With many short examples, I will show you how commondesign patterns are used routinely in the .NET Framework and how you shouldemploy them in your own designs.In this book I’ll explore with you all the new features of the C# 3.0 languagein detail, including extension methods, lambda expressions, and LanguageIntegrated Query (LINQ) among others. These are all exciting additions to C# 3.0that foster a functional programming model within what has previously beenalmost exclusively an imperative programming language. As you put these newfeatures of C# 3.0 to use, you’ll find that they expand your horizons and openup new possibilities of solutions using functional programming techniques!Another area of particular interest for C# developers is how we can writeexception-safe and fault-tolerant code. The .NET Framework includes severalfacilities, including Critical Execution Regions, to help protect the state of yourapplication in the event of an asynchronous exception. In this book, I explainhow to use these new facilities to your advantage.Have fun programming and remember, always defining a contract beforeimplementation, striving to be exception-neutral, and being frugal with yourresource usage are all keys to entering the halls of C# guru code-dom.Trey NashFree Companion eBookTHE APRESS ROADMAPBeginning C# 2008:From Novice to ProfessionalFor a limited time only.See last page for details.SOURCE CODE ONLINEwww.apress.comAccelerated C# 2008BeginningC# 2008 Databases:From Novice to ProfessionalPro C# 2008 and the.NET 3.5 Framework, 4ePro LINQ:Language IntegratedQuery in C# 2008Pro WPF in C# 2008:Windows PresentationFoundation with .NET 3.5, 2ePro WCF: Practical MicrosoftSOA ImplementationIllustrated C# 2008Accelerated C# 2008Dear Reader,Author ofThe EXPERT’s VOIce in .NETUS 39.99SeeSee lastlast pagepage forfor details.details.OfferOffer endsends JuneJune 30,30, 2008.2008.AcceleratedC# 2008Trey NashISBN-13: 978-1-59059-873-3ISBN-10: 1-59059-873-353999For a limited time,get the free, fully searchableeBook—a 20 value!NashForeword by Wes DyerC# Language Design Team, MicrosoftShelve inProgramming Languages/C#User level:Intermediate–Advanced9 781590 598733this print for content only—size & color not accuratespine 1.028" 544 page count

8733fmCMP4.qrk10/16/0711:04 AMPage iAccelerated C# 2008Trey Nash

8733fmCMP4.qrk10/16/0711:04 AMPage iiAccelerated C# 2008Copyright 2007 by Weldon W. Nash, IIIAll 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 retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-59059-873-3ISBN-10 (pbk): 1-59059-873-3Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrenceof a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.Lead Editor: Dominic ShakeshaftTechnical Reviewer: Shawn WildermuthEditorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom WelshProject Manager: Sofia MarchantCopy Editor: Jim ComptonAssistant Production Director: Kari Brooks-CoponyProduction Editor: Laura CheuCompositor: Jimmie YoungProofreader: April EddyIndexer: Beth PalmerArtist: April MilneCover Designer: Kurt KramesManufacturing Director: Tom DebolskiDistributed 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, orvisit http://www.springeronline.com.For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visithttp://www.apress.com.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 anyliability to any person or entity with respect to any loss or damage caused or alleged to be caused directlyor indirectly by the information contained in this work.The source code for this book is available to readers at http://www.apress.com.

8733fmCMP4.qrk10/16/0711:04 AMPage iiiThis book is dedicated to the memory of my grandfatherWeldon W. Nash, Sr.December 19, 1912 – April 29, 2007To Svetlanafor believing in me

8733fmCMP4.qrk10/16/0711:04 AMPage iv

8733fmCMP4.qrk10/16/0711:04 AMPage vContents at a GlanceForeword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiAbout the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiiiIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv CHAPTER 1C# Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 CHAPTER 2C# and the CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 CHAPTER 3C# Syntax Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 CHAPTER 4Classes, Structs, and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 CHAPTER 5Interfaces and Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 CHAPTER 6Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 CHAPTER 7Exception Handling and Exception Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 CHAPTER 8Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 CHAPTER 9Arrays, Collection Types, and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 CHAPTER 10Delegates, Anonymous Functions, and Events . . . . . . . . . . . . . . . . . . . . . . . 253 CHAPTER 11Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 CHAPTER 12Threading in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 CHAPTER 13In Search of C# Canonical Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 CHAPTER 14Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 CHAPTER 15Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 CHAPTER 16LINQ: Language Integrated Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 APPENDIXReferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497v

8733fmCMP4.qrk10/16/0711:04 AMPage vi

8733fmCMP4.qrk10/16/0711:04 AMPage viiContentsForeword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiAbout the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiiiIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv CHAPTER 1C# Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Differences Between C# and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1C# 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2CLR Garbage Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Example of a C# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Overview of Features Added in C# 2.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Overview of What’s New in C# 3.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 2C# and the CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9The JIT Compiler in the CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Assemblies and the Assembly Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Minimizing the Working Set of the Application . . . . . . . . . . . . . . . . . . . . . . . 11Naming Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Loading Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Cross-Language Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 CHAPTER 3C# Syntax Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17C# Is a Strongly Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19vii

8733fmCMP4.qrkviii10/16/0711:04 AMPage viii CONTENTSTypes and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Default Variable Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Implicitly Typed Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27as and is Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Defining Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Using Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35if-else, while, do-while, and for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35foreach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36break, continue, goto, return, and throw . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 CHAPTER 4Classes, Structs, and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57sealed Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64abstract Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Indexers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69partial Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70partial Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Static Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Reserved Member Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Value Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76The Meaning of this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Anonymous Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Object Initializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

8733fmCMP4.qrk10/16/0711:04 AMPage ix CONTENTSBoxing and Unboxing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87When Boxing Occurs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Efficiency and Confusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93System.Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Equality and What It Means . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95The IComparable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95The new Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Field Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97Static (Class) Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Instance Constructor and Creation Ordering . . . . . . . . . . . . . . . . . . . . . 101Destroying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Deterministic Destruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Exception Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Disposable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107The IDisposable Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107The using Keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Method Parameter Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Value Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111ref Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112out Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113param Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Inheritance and Virtual Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Virtual and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115override and new Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115sealed Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118A Final Few Words on C# Virtual Methods . . . . . . . . . . . . . . . . . . . . . . . 118Inheritance, Containment, and Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . 118Choosing Between Interface and Class Inheritance. . . . . . . . . . . . . . . . 119Delegation and Composition vs. Inheritance . . . . . . . . . . . . . . . . . . . . . 120Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 CHAPTER 5Interfaces and Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Interfaces Define Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Defining Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125What Can Be in an Interface? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Interface Inheritance and Member Hiding . . . . . . . . . . . . . . . . . . . . . . . 126ix

8733fmCMP4.qrkx10/16/0711:04 AMPage x CONTENTSImplementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Implicit Interface Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Explicit Interface Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Overriding Interface Implementations in Derived Classes . . . . . . . . . . . 131Beware of Side Effects of Value Types Implementing Interfaces . . . . . . 134Interface Member Matching Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Explicit Interface Implementation with Value Types . . . . . . . . . . . . . . . . . . . . 139Versioning Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Contracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Contracts Implemented with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 142Interface Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Choosing Between Interfaces and Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . 144Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 CHAPTER 6Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149Just Because You Can Doesn’t Mean You Should . . . . . . . . . . . . . . . . . . . . . . 149Types and Formats of Overloaded Operators . . . . . . . . . . . . . . . . . . . . . . . . . 149Operators Shouldn’t Mutate Their Operands . . . . . . . . . . . . . . . . . . . . . . . . . 150Does Parameter Order Matter? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Overloading the Addition Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Operators That Can Be Overloaded . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Conversion Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 CHAPTER 7Exception Handling and Exception Safety . . . . . . . . . . . . . . . . . . . 163How the CLR Treats Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Mechanics of Handling Exceptions in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164Changes with Unhandled Exceptions Starting with .NET 2.0 . . . . . . . . . 164Syntax Overview of the try Statement. . . . . . . . . . . . . . . . . . . . . . . . . . 165Rethrowing Exceptions and Translating Exceptions. . . . . . . . . . . . . . . . 167Exceptions Thrown in finally Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Exceptions Thrown in Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Exceptions Thrown in Static Constructors. . . . . . . . . . . . . . . . . . . . . . . 171Who Should Handle Exceptions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Avoid Using Exceptions to Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173Achieving Exception Neutrality. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173Basic Structure of Exception-Neutral Code . . . . . . . . . . . . . . . . . . . . . . 174Constrained Execution Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Critical Finalizers and SafeHandle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

8733fmCMP4.qrk10/16/0711:04 AMPage xi CONTENTSCreating Custom Exception Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185Working with Allocated Resources and Exceptions. . . . . . . . . . . . . . . . . . . . . 186Providing Rollback Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 CHAPTER 8Working with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195String Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Format Specifiers and Globalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197Object.ToString, IFormattable, and CultureInfo . . . . . . . . . . . . . . . . . . . 198Creating and Registering Custom CultureInfo Types . . . . . . . . . . . . . . . 199Format Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Console.WriteLine and String.Format . . . . . . . . . . . . . . . . . . . . . . . . . . 202Examples of String Formatting in Custom Types . . . . . . . . . . . . . . . . . . 203ICustomFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Working with Strings from Outside Sources . . . . . . . . . . . . . . . . . . . . . . . . . . 208StringBuilder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209Searching Strings with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 211Searching with Regular Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . 211Searching and Grouping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Replacing Text with Regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216Regex Creation Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 CHAPTER 9Arrays, Collection Types, and Iterators . . . . . . . . . . . . . . . . . . . . . . 221Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221Implicitly Typed Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Type Convertibility and Covariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Sortability and Searchability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225Vectors vs. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226Multidimensional Rectangular Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Multidimensional Jagged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229Collection Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Comparing ICollection with ICollection. . . . . . . . . . . . . . . . . . . . . . . . . 231Collection Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235System.Collections.ObjectModel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238xi

8733fmCMP4.qrkxii10/16/0711:04 AMPage xii CONTENTSIEnumerable T , IEnumerator T , IEnumerable, and IEnumerator . . . . . . . 239Types That Produce Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242Forward, Reverse, and Bidirectional Iterators . . . . . . . . . . . . . . . . . . . . 247Collection Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 CHAPTER 10Delegates, Anonymous Functions, and Events . . . . . . . . . . . . . . 253Overview of Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Delegate Creation and Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Single Delegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255Delegate Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256Iterating Through Delegate Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Unbound (Open Instance) Delegates . . . . . . . . . . . .

Pro LINQ: Language Integrated Query in C# 2008 Pro C# 2008 and the.NET 3.5 Framework, 4e Illustrated C# 2008 Pro WCF: Practical Microsoft SOA Implementation Beginning C# 2008: From Novice to Professional Beginning C# 2008 Databases: From Novice to Professional Accelerated C# 2008 Pro WPF in C# 2008: Windows Presentation Foundation with .NET 3.5, 2e

Related Documents:

RPS or RPS‑LITE Remote Programming Software Readers ARD‑R10 iCLASS Mullion Reader ARD‑R40 iCLASS Switchplate Reader ARD‑RK40‑09 iCLASS PIN Reader ARD‑VSMART iCLASS Reader D8223 Prox Pro Reader D8224 Mullion Reader D8224‑SP Switch Plate Reader D8225 Mini Mullion Reader D8301W L

Readers Theatre Scripts Family Tutoring 584 Come Hippopotamus Roles: Reader 1, All, Reader 2, Reader 3 Reader 1 Come hippopotamus All HIP HIP HIP! HIP HIP HIP! Reader 2 What an enormous face you have! Reader 3 What an enormous lip! Reader 1 Can't you come and play a bit? All Dance! Dance! Reader 2 And hop!

Conc Diagram- All Failures Cispa Data Avalon Date PCB Lot Number Igarashi TPS date codes. MB Panel Number VIAS Hole Location Failure 9-June'09 11-June'09 923 162 12-June'09 923 163,164 TBD TBD 1 13-June'09 923 164 15-June'09 923 166 16-June'09 923 167 17-June'09 923 168,171 18-June'09 923 171 19-June'09 923 171 20-June'09 923 171 22-June'09 923 173 23-June '09 923 179 24-June '09 923 .

Zephyr Environmental Corporation Term Ends: 2013 Mingming Lu, Vice Chair University of Cincinnati Term Ends: 2013 John D. Bachmann Vision Air Consulting Term Ends: 2012 Jane C. Barton Patterson Consultants Term Ends: 2012 Gary Bramble, P.E. Dayton Power and Light Term Ends: 2014 Prakash Doraiswamy, Ph.D. RTI International Term Ends: 2014 Steven .

2019 TRUST COMMITTEE Mike Welker 1st term ends 2/2020 Vern Nielsen 1st term ends 2/2021 Ron Gruss 1st term ends 2/2022 Annette Lawson 1st term ends 2/2023 Ed Engle 1st term ends 2/2024 2019 FLBC REPRESENTATIVES Bruce Graving, Ruthe Meeks 2019 AUDIT COMMITTEE Ginger DeWitt 1st term ends 2/2020

the ends, ways, and means designed to achieve a certain goal. Mathematically, we might express this as "Strategy Ends Ways Means." Ends are the objectives or desired outcomes of a given strat-egy. the term end-state is synonymous with ends. an end or ends comprise the goal of the strategy. Ways are actions. they are the methods and .

3.2. All GLog : export general log data without delete the log data on the reader. 3.3. Slog&Del : export System log in the reader and delete the log data on the reader. 3.4. All Slog : export System log in the reader without delete the log data on the reader. 3.5. Enr.Data : export Enroll Data in the reader (Finger and ID Card Register ) 3.6.

June 16 Shelach Lecha June 23 - Korach June 30 Hukath We Remember Sylvia Marans Elberg - June Vera Meyerhoff - June Daniel Rosenberg - June1 Edward Wandrei - June 6 Helen Feinberg-Ginsburg - June 6 Thelma Cohn - June 12 Wilma Sizemore June - 16 Joseph P. Suffel - June 21 Eliot Rivers - June 31 Paul Aaron Kowarsky - Sivan 14 -