Programming Microsoft Visual C# 2005: The Language byDonis Marshall Microsoft Press 2006 (704 pages) ISBN:0735621810 Offering expert guidance, hands-on programming instruction, and practical examples, this straightforward book will help both new and experienced developers advance their proficiency in developing applications for Microsoft Windows and the Web Table of Contents Programming Microsoft Visual C# 2005—The Language Introduction Part I - Core Language Chapter 1 - Introduction to Visual C# Programming Chapter 2 - Types Chapter 3 - Inheritance Part II - Core Skills Chapter 4 - Introduction to Visual Studio 2005 Chapter 5 - Arrays and Collections Chapter 6 - Generics Chapter 7 - Iterators Part III - More C# Language Chapter 8 - Delegates and Events Chapter 9 - Exception Handling Part IV - Debugging Chapter 10 - Metadata and Reflection Chapter 11 - MSIL Programming Chapter 12 - Debugging with Visual Studio 2005 Chapter 13 - Advanced Debugging Part V - Advanced Concepts Chapter 14 - Memory Management Chapter 15 - Unsafe Code Appendix A - Operator Overloading Index List of Figures List of Tables Back Cover Get hands-on, expert guidance for developing with Visual C# 2005 Get the essential, straightforward information you need to master the core capabilities of Visual C# 2005 Both new and experienced developers get expert guidance, hands-on programming instruction, and practical examples to help advance their proficiency in developing applications for Microsoft Windows and the Web Discover how to: Refine class usage with inheritance, polymorphism, and other strategies Implement generics to define a type-safe data structure Work with stacks, queues, arrays, dictionaries, and other collections Use iterators to implement and standardize enumerator patterns Know when to catch exceptions—and handle them locally or propagate them Interrogate metadata and facilitate late binding by using reflection Synchronize threads with locks, events, mutexes, and other tools Use the Microsoft Visual Studio Debugger and explore advanced debugging techniques and tools About the Author Donis Marshall is a trainer, a consultant and an author with 20 years of development experience and an indepth background on Microsoft NET technologies He is the author of several books, including NET Security Programming Donis teaches classes on NET programming, debugging, security, and design and architecture In addition, he is the president of DebugNow Programming Microsoft Visual C# 2005—The Language Donis Marshall PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2006 by Donis Marshall ISBN:0735621810 All rights reserved No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher Library of Congress Control Number 2005934153 Printed and bound in the United States of America 1 2 3 4 5 6 7 8 9 QWT 0 9 8 7 6 5 Distributed in Canada by H.B Fenn and Company Ltd A CIP catalogue record for this book is available from the British Library Microsoft Press books are available through booksellers and distributors worldwide For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329 Visit our Web site at www.microsoft.com/mspress Send comments to mspinput@microsoft.com Microsoft, IntelliSense, Microsoft Press, MSDN, Visual Basic, Visual C#, Visual Studio, the Visual Studio logo, Win32, Windows, Windows CE, the Windows logo, Windows NT, and WinFX are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Other product and company names mentioned herein may be the trademarks of their respective owners The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred This book expresses the author's views and opinions The information contained in this book is provided without any express, statutory, or implied warranties Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book Acquisitions Editor: Ben Ryan Project Editor: Valerie Woolley Technical Editor: James D Rogers Copy Editor: Nancy Sixsmith Indexer: Tony Ross and Lee Ross Body Part No X11-50074 This book is dedicated to my father, Herbert Marshall He was a nuclear engineer, an artist, a confidant, a doting husband to my mother, and most of all a compassionate person He was a giant amongst men and touched innumerable lives His three sons, including myself, miss him every day Acknowledgments Programming Microsoft Visual C# 2005: The Language was completed with the collaboration of several people I am credited on the cover, but the contribution of others is no less important I want to especially acknowledge the contributions of Valerie Woolley, Ben Ryan, and Jim Rogers If possible, they would also receive cover credit Valerie was the project manager and provided support, encouragement, patience, and the occasional nudge when necessary Ben, who was the acquisitions editor, had unyielding confidence in my ability, which is greatly appreciated Jim Rogers was the technical editor and diligently reviewed hundreds of pages of manuscript and code He helped assure the superb quality of the book I also want to thank John Bruno, who is an exceptional engineer, for reviewing and commenting on each chapter, which was very helpful Writing a book is an all-consuming project Unfortunately, much of this burden is carried by friends and loved ones I appreciate the patience and support of my mother, Lynn, and of my friends Herb, Jr., Chuck, and Patty, along with a long list of other friends who were treated shabbily during this project Thanks for your understanding Finally, I want to acknowledge my children: Jason, Kristen, and Adam They make every day special They are my motivation A special acknowledgment is reserved for Jason During most of this project, Jason was stationed in Iraq as a Marine He recently returned home safely We are proud of you Donis Marshall Donis Marshall is currently one of the few trainers endorsed by Microsoft Global Learning Services to conduct Microsoft technology classes for Microsoft employees In this capacity, Mr Marshall travels internationally, delivering dozens of classes to Microsoft developers and engineers in the United States, Europe, and Asia His repertoire includes classes on Advanced NET UMD Debugging, Advanced NET Debugging workshops, NET Design and Architecture, Visual Basic NET Programming, NET Interoperability and Security, NET Web Services, and ASP.NET He also teaches NET classes at Autodesk, NCCI, and NASA Donis Marshall is a nationally recognized teacher of computer technology to developers and scientists As founder and lead instructor for The Training Alliance, he taught advanced technical classes for many Fortune 500 clients He also managed a staff of technical instructors as Director of Advanced Technical Learning Services at Productivity Point International, a national franchiser of training services Mr Marshall is President of DebugNow (www.debugnow) DebugNow offers an assortment of innovative tools for support engineers and developers to debug and monitor Win32 and NET applications As a contractor, Donis Marshall has written thousands of lines of code for various entities Introduction Microsoft Visual C# 2005 includes several enhancements to earlier versions of the language If you want to learn the new features of the language, Programming Microsoft Visual C# 2005: The Language offers detailed explanations of each improvement One of the most awaited additions to the NET environment is generics Other additions include anonymous methods, static classes, and new classes that affect garbage collection This book introduces these new features, provides context, and displays sample code Enhancements are not limited to the language The Microsoft Visual Studio IDE has also been enhanced in Visual Studio 2005 Microsoft continues to expand upon the impressive assortment of rapid application development (RAD) tools that are available The Exception Assistant, code snippets, and visualizers are some of the more notable advancements, where the emphasis is on providing helpful information that markedly increases developer productivity This book details these and other improvements to previous versions of Visual Studio A larger portion of the lifetime of an application is spent in maintenance and debugging Efficient and effective debugging tools and techniques can facilitate a more robust application, which reduces the need to debug You can also resolve problems more quickly when they inevitably occur A managed application has a managed veneer and an unmanaged underpinning Debugging requires an understanding of both realms where a managed application exists: managed and unmanaged From a debugging context, Chapter 12 examines both realms of a managed application and helps developers understand how to effectively debug a managed application Who Is This Book For? Programming Microsoft Visual C# 2005: The Language is for developers who want a comprehensive explanation of Visual C# 2005 or want to explore a specific aspect of the language It is a composite of chapters sequenced to provide a rational and complete review of the language Each chapter is also reviewable as an independent unit that encapsulates a specific topic This book targets both professional and casual developers Practical, indepth explanations are offered for even the most ardent developers Sample code is provided as a complement to the content For casual developers, code is often the clearest explanation of in-depth concepts Actually, even for professional developers, sample code is often invaluable For this reason, this book contains reams of code examples Chapter 3: Inheritance Table 3-1: Inheritance Terminology Table 3-2: System.Object Methods Table 3-3: Member Accessibility Chapter 4: Introduction to Visual Studio 2005 Table 4-1: Project Types Table 4-2: Web Project Types Table 4-3: Special-Purpose Project Templates Table 4-4: Default Code Snippets Table 4-5: Refactoring Operations Table 4-6: Reserved Properties Table 4-7: Default Tasks Table 4-8: MSBuild Switches Chapter 5: Arrays and Collections Table 5-1: Interfaces Implemented at System.Array Table 5-2: System.Array Members Table 5-3: System.Array Properties Table 5-4: List Members Table 5-5: Collection Types Table 5-6: ArrayList Members Table 5-7: BitArray Members Table 5-8: Hashtable Members Table 5-9: Queue Members Table 5-10: SortedList Members Table 5-11: Stack Members Table 5-12: Specialized Collections Chapter 6: Generics Table 6-1: Inheritance Table for Generic Types Table 6-2: Combination of Overriding Generic Methods Table 6-3: Generic Types and Interfaces Chapter 7: Iterators Table 7-1: Enumerator States Chapter 8: Delegates and Events Table 8-1: Delegate Members Table 8-2: IAsyncResult Properties Chapter 9: Exception Handling Table 9-1: Exception Methods Table 9-2: Exception Properties Table 9-3: COM Exception Table Table 9-4: IErrorInfo to COMException Mapping Chapter 10: Metadata and Reflection Table 10-1: Metadata Tables Table 10-2: Metadata Streams Table 10-3: PEVerify Options Table 10-4: Elements of ILDASM Table 10-5: ILDASM Options Table 10-6: MemberTypes Enumeration Table 10-7: Type Methods That Return Metadata Collections Table 10-8: Abstract Methods of the Binder Class Table 10-9: ReflectionPermission Flags Chapter 11: MSIL Programming Table 11-1: Hello World MSIL Code Table 11-2: Assembly Directives Table 11-3: Class Directives Table 11-4: Directives Included in the Method Block Table 11-5: Load Instructions Table 11-6: Store Instructions Table 11-7: Compare Instructions Table 11-8: Comparative Branching Table 11-9: Call Instructions Table 11-10: Syntax for Defining Arrays Table 11-11: Arithmetic Instructions Table 11-12: Exception Clauses Table 11-13: Miscellaneous MSIL Instructions Chapter 12: Debugging with Visual Studio 2005 Table 12-1: Control of a Debugging Session Table 12-2: General Debugging Options Table 12-3: Hit Count Options Table 12-4: Filter Contexts Table 12-5: Tracepoint Keywords Table 12-6: Breakpoint Symbols Table 12-7: Navigating the Immediate Window Table 12-8: Command Aliases Table 12-9: Modules Window Columns Table 12-10: Processes Window Columns Table 12-11: Registers Table 12-12: TraceSource Methods Table 12-13: TraceSource Properties Table 12-14: Trace Levels Table 12-15: Activity Traces Table 12-16: Source Level Table 12-17: SourceSwitch Properties Table 12-18: Trace Listeners Table 12-19: TraceListener Properties Table 12-20: TraceOptions Values Table 12-21: DebuggableBrowserState Values Chapter 13: Advanced Debugging Table 13-1: MDbg Commands Table 13-2: Basic WinDbg Commands Table 13-3: Stack Trace Commands Table 13-4: Common Display Memory Commands Table 13-5: Common Display Memory Commands Table 13-6: WinDbg Directives Table 13-7: Son of Strike Commands Table 13-8: Item, LargeItem, and largeStuff Instances Table 13-9: Performance Monitor Counters Chapter 14: Memory Management Table 14-1: WeakReference Class Table 14-2: GC Class Chapter 15: Unsafe Code Table 15-1: Pointer Symbols Table 15-2: CallingConvention Enumeration Table 15-3: CharSet Enumeration Table 15-4: LayoutKind Enumeration Table 15-5: Directional Attributes and C# Keywords Table 15-6: UnmanagedType Enumeration Appendix A: Operator Overloading Table A-1: Replacement Methods for Mathematical and Logical Operators Table A-2: Replacement Methods for Relational Operators ... five parts The first part, "Core Language, " introduces the basic concepts of the language Chapter 1 contains a general overview of the language Chapter 2 introduces types, which include classes and value types... Every effort has been made to ensure the accuracy of this book and the companion content As corrections or changes are collected, they will be added to a Microsoft Knowledge Base article To view the list of known corrections for this book, visit the following article:... Info these links The current ECMA standards for the C# Language Specification:http://www.ecmainternational.org/publications/standards/Ecma-334.htm The current ECMA standards for the Common Language