Pro C# with .NET 3.0 Special Edition
Total Page:16
File Type:pdf, Size:1020Kb
Pro C# with .NET 3.0 Special Edition Andrew Troelsen Pro C# with .NET 3.0, Special Edition Copyright © 2007 by Andrew Troelsen 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: 978-1-59059-823-8 ISBN-10: 1-59059-823-7 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. Lead Editor: Ewan Buckingham Technical Reviewer: Christophe Nasarre Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Matt Wade Project Manager: Grace Wong Copy Edit Manager: Nicole Flores Copy Editors: Nicole Flores, Ami Knox Assistant Production Director: Kari Brooks-Copony Senior Production Editor: Kelly Winquist Compositor: Dina Quan Proofreader: Linda Seifert Indexer: Broccoli Information Management 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 [email protected],or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion 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 in the Source Code/ Download section. You will need to answer questions pertaining to this book in order to successfully download the code. I would like to dedicate this book to my mother, Mary Troelsen. Mom, thanks for all of your support over the years and the years to come. Oh yeah, and thanks for not busting my chops when I came home with the red Mohawk. Luv ya, Pooch Contents at a Glance About the Author ....................................................................xxxvii About the Technical Reviewer ..........................................................xxxix Acknowledgments .....................................................................xli Introduction ..........................................................................xliii PART 1 ■■■Introducing C# and the .NET Platform ■CHAPTER 1 The Philosophy of .NET .............................................3 ■CHAPTER 2 Building C# Applications............................................33 PART 2 ■■■ The C# Programming Language ■CHAPTER 3 C# Language Fundamentals ........................................65 ■CHAPTER 4 Object-Oriented Programming with C# 2.0..........................139 ■CHAPTER 5 Understanding Object Lifetime .....................................179 ■CHAPTER 6 Understanding Structured Exception Handling ......................197 ■CHAPTER 7 Interfaces and Collections .........................................221 ■CHAPTER 8 Callback Interfaces, Delegates, and Events .........................255 ■CHAPTER 9 Advanced C# Type Construction Techniques ........................289 ■CHAPTER 10 Understanding Generics ...........................................321 PART 3 ■■■Programming with .NET Assemblies ■CHAPTER 11 Introducing .NET Assemblies ......................................347 ■CHAPTER 12 Type Reflection, Late Binding, and Attribute-Based Programming ....391 ■CHAPTER 13 Processes, AppDomains, Contexts, and CLR Hosts ..................425 ■CHAPTER 14 Building Multithreaded Applications................................449 ■CHAPTER 15 Understanding CIL and the Role of Dynamic Assemblies .......................................................477 iv PART 4 ■■■Programming with the .NET Libraries ■CHAPTER 16 The System.IO Namespace ........................................517 ■CHAPTER 17 Understanding Object Serialization.................................545 ■CHAPTER 18 The .NET Remoting Layer..........................................565 ■CHAPTER 19 Building a Better Window with System.Windows.Forms .............605 ■CHAPTER 20 Rendering Graphical Data with GDI+ ...............................649 ■CHAPTER 21 Programming with Windows Forms Controls........................699 ■CHAPTER 22 Database Access with ADO.NET....................................759 PART 5 ■■■Web Applications and XML Web Services ■CHAPTER 23 ASP.NET 2.0 Web Pages and Web Controls .........................829 ■CHAPTER 24 ASP.NET 2.0 Web Applications .....................................889 ■CHAPTER 25 Understanding XML Web Services..................................919 PART 6 ■■■Programming with .NET 3.0 Extensions ■CHAPTER 26 Establishing a .NET 3.0 Programming Environment..................957 ■CHAPTER 27 Introducing Windows Presentation Foundation ......................969 ■CHAPTER 28 Introducing Windows Communication Foundation ..................1021 ■CHAPTER 29 Introducing Windows Workflow Foundation ........................1047 ■CHAPTER 30 C# 3.0 Language Features........................................1075 ■CHAPTER 31 An Introduction to LINQ...........................................1107 ■INDEX ...........................................................................1151 v Contents About the Author ....................................................................xxxvii About the Technical Reviewer ..........................................................xxxix Acknowledgments .....................................................................xli Introduction ..........................................................................xliii PART 1 ■■■Introducing C# and the .NET Platform ■CHAPTER 1 The Philosophy of .NET .........................................3 Understanding the Previous State of Affairs ...............................3 Life As a C/Win32 API Programmer . 3 Life As a C++/MFC Programmer ..................................4 Life As a Visual Basic 6.0 Programmer ..............................4 Life As a Java/J2EE Programmer...................................4 Life As a COM Programmer .......................................5 Life As a Windows DNA Programmer ...............................5 The .NET Solution.....................................................6 Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS) ............................................6 The Role of the Base Class Libraries ................................7 What C# Brings to the Table ............................................7 Additional .NET-Aware Programming Languages...........................8 Life in a Multilanguage World .....................................9 An Overview of .NET Assemblies .......................................10 Single-File and Multifile Assemblies ....................................11 The Role of the Common Intermediate Language .........................11 Benefits of CIL .................................................13 Compiling CIL to Platform-Specific Instructions ......................14 The Role of .NET Type Metadata .......................................14 The Role of the Assembly Manifest .....................................15 Understanding the Common Type System ...............................15 CTS Class Types ...............................................16 CTS Structure Types ............................................16 CTS Interface Types.............................................17 vii viii ■CONTENTS CTS Enumeration Types .........................................17 CTS Delegate Types ............................................17 CTS Type Members .............................................18 Intrinsic CTS Data Types.........................................18 Understanding the Common Language Specification ......................19 Ensuring CLS Compliance .......................................20 Understanding the Common Language Runtime ..........................20 The Assembly/Namespace/Type Distinction .............................22 Accessing a Namespace Programmatically .........................24 Referencing External Assemblies .................................25 Using ildasm.exe ....................................................26 Viewing CIL Code ...............................................27 Viewing Type Metadata..........................................28 Viewing Assembly Metadata......................................28 Deploying the .NET Runtime ...........................................29 The Platform-Independent Nature of .NET ...............................29 Summary ..........................................................31 ■CHAPTER 2 Building C# Applications.......................................33 Installing the .NET Framework 2.0 SDK..................................33 The C# Command-Line Compiler