CYAN MAGENTA YELLOW BLACK PANTONE 123 C Books for professionals by professionals ® Free Companion eBook Available Accelerated C# 2008 Accelerated C# 2005 Coauthor of Accelerated VB 2005 Accelerated VB 2008 In your hands is a guide to creating effective C# 3.0 code This book focuses precisely on the C# 3.0 language, showing you how to write programs that are robust, fault-tolerant, and ready to be put into widely available libraries Accelerated C# 2008 won’t burden your time with endless discussions of libraries 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 design principles developed in the object oriented arena 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 In this book I’ll explore with you all the new features of the C# 3.0 language in detail, including extension methods, lambda expressions, and Language Integrated Query (LINQ) among others These are all exciting additions to C# 3.0 that foster a functional programming model within what has previously been almost exclusively an imperative programming language As you put these new features of C# 3.0 to use, you’ll find that they expand your horizons and open up new possibilities of solutions using functional programming techniques! Another area of particular interest for C# developers is how we can write exception-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 new facilities to your advantage Have fun programming and remember, always defining a contract before implementation, striving to be exception-neutral, and being frugal with your resource usage are all keys to entering the halls of C# guru code-dom Trey Nash Free Companion eBook THE APRESS ROADMAP Beginning C# 2008: From Novice to Professional For a limited time only See last page for details SOURCE CODE ONLINE www.apress.com Accelerated C# 2008 Beginning C# 2008 Databases: From Novice to Professional Pro C# 2008 and the NET 3.5 Framework, 4e Pro LINQ: Language Integrated Query in C# 2008 Pro WPF in C# 2008: Windows Presentation Foundation with NET 3.5, 2e Pro WCF: Practical Microsoft SOA Implementation Illustrated C# 2008 Accelerated C# 2008 Dear Reader, Author of The EXPERT’s VOIce ® in NET US $39.99 See See last last page page for for details details Offer Offer ends ends June June 30, 30, 2008 2008 Accelerated C# 2008 Trey Nash ISBN-13: 978-1-59059-873-3 ISBN-10: 1-59059-873-3 53999 For a limited time, get the free, fully searchable eBook—a $20 value! Nash Foreword by Wes Dyer C# Language Design Team, Microsoft Shelve in Programming Languages/C# User level: Intermediate–Advanced 781590 598733 this print for content only—size & color not accurate spine = 1.028" 544 page count 8733fmCMP4.qrk 10/16/07 11:04 AM Page i Accelerated C# 2008 Trey Nash 8733fmCMP4.qrk 10/16/07 11:04 AM Page ii Accelerated C# 2008 Copyright © 2007 by Weldon W Nash, III 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-59059-873-3 ISBN-10 (pbk): 1-59059-873-3 Printed and bound in the United States of America 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 Lead Editor: Dominic Shakeshaft Technical Reviewer: Shawn Wildermuth Editorial 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 Welsh Project Manager: Sofia Marchant Copy Editor: Jim Compton Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Cheu Compositor: Jimmie Young Proofreader: April Eddy Indexer: Beth Palmer Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski 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 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 visit http://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 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 8733fmCMP4.qrk 10/16/07 11:04 AM Page iii This book is dedicated to the memory of my grandfather Weldon W Nash, Sr December 19, 1912 – April 29, 2007 To Svetlana for believing in me 8733IndCMP3.qrk 10/17/07 9:52 AM Page 509 ■INDEX ■U uint type, 20, 22 ulong type, 20, 22 unary operators, 150, 153 unbound delegates, 259–262, 276 UnboundDelegate, 276 unboxing, 87–93 see also boxing/unboxing undefined behavior exception, 107 unhandled exceptions, 164 UnhandledException, 164 UnhandledExceptionEventArgs, 164 unheld thread mode, 348 UnloadException, 165 unsafe code techniques, 24 unspeakable field names, 2, 82 Unstarted state, thread, 322 upgradeable thread mode, 348 UpgradeToWriterLock method, 347 ushort type, 20, 22 using directive, using keyword deterministic destruction, 190, 376 disposable objects, 375 extension methods, 418 namespaces, 34 overloading, 19, 110 ■V Validate method, 186 value arguments, 111 value equality, 382, 385 value keyword, 265 value types C# support, 21 canonical forms, 405–413 checklist, 363, 415 definition, 40, 75–81 described, 87 finalizers, 406 ICloneable interface, 406 IComparable interface, 406, 411 IConvertible interface, 406 IDisposable interface, 406 IFormattable interface, 406 interface implementation, 134, 139–141, 410, 411 new keyword, 96 terminology, 24 type safety, 411–413 uses, ValueType class, 410 ValueType.Equals method, 385 var keyword, 6, 26 VarArgs method, 114 variables see also data types capture, 5, 270–272, 436, 456–460 default, initialization, 292 implicit typing, 6, 25–26, 450 naming, LINQ, 25 Find it faster at http://superindex.apress.com/ threads aborting, 322 background, 326 creating, 318 data storage, 327–330 Dispose method, 375 exceptions, 348 foreground, 326 modes, 348 NET and, 3, 17, 331 recursion, 348 states, 320, 322, 323, 330, 348 synchronizing, 331–353 terminating/halting, 323–325 unmanaged, 330 waking, 324 Thread.Sleep method, 322, 324, 332, 335 ThreadStart delegate, 318 ThreadState enumeration, 322 ThreadStaticAttribute attribute, 327 Thread.Suspend method, 323, 325, 332 throw statement, 36, 164, 167, 174, 176 throwing exceptions, 164–172 thunks, 253 Timer class, 361, 362 TimerProc method, 362 timers, 361–362 TInput placeholder identifier, 288 TLSClass, 328 TLSFieldClass, 328 ToArray extension method, 494 ToList extension method, 485 ToString method, 95, 198, 201–202, 204, 393–396, 397 TOutput placeholder identifier, 288 transforms, 425–429 translating exceptions, 168 Troelsen, Andrew, try statement, 165, 173 try/catch statement, 19 try/finally statement, 19, 109, 242 TryGetValue method, 234 type casting conversion operators, 58, 149, 153, 156–158, 398 explicit, 27, 40 generic, 3–14, 291, 305 implicit, 27, 40, 58 from larger to smaller, 156 Type Library (TLB), 14 type safety, 281, 399–403, 411–413 TypeConverter class, 398 TypeInitializationException, 171, 185 typeof keyword, 14 typeof operator, 315 types, data see data types 509 8733IndCMP3.qrk 510 10/17/07 9:52 AM Page 510 ■INDEX vectors, arrays compared, 226 verbatim strings, 196 Vermeulen, Allan, 320 versioning, 13, 141 virtual keyword, 115–116, 133 virtual members, 62 virtual methods, 103, 115–118, 264, 366, 367 Visitor pattern, 437–440 Visual Basic collection types, 235 Visual Studio code analysis, 27 Vlissides, John, 123, 366, 437 volatile field modifier, 44 vtables, interface tables compared, 137 ■W Wait method, 322, 342 WaitAll method, 351 WaitAny method, 351 WaitHandle class, 349–353 WaitOne method, 349–350 WaitSleepJoin state, thread, 322 waking threads, 324 where clause (LINQ), 475, 483 where keyword, 299 Where method, 474 while statement, 19, 35 Win32 functions, 184 Win32 Synchronization objects, 351 write thread mode, 348 write-only properties, 48 ■Y Y fixed-point combinators, 463 “Yet Another Language Geek” blog, 437 yield blocks, 243, 244–245, 247–248, 251, 482 yield break statement, 244 yield keyword, 237, 243, 288 ... Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Sofia Marchant Copy Editor: Jim Compton Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Cheu Compositor:... C# support, 21 canonical forms, 405–413 checklist, 363, 415 definition, 40, 75–81 described, 87 finalizers, 406 ICloneable interface, 406 IComparable interface, 406, 411 IConvertible interface,...8733fmCMP4.qrk 10/16/07 11:04 AM Page i Accelerated C# 2008 Trey Nash 8733fmCMP4.qrk 10/16/07 11:04 AM Page ii Accelerated C# 2008 Copyright © 2007 by Weldon W Nash,