Upload
trinhque
View
250
Download
0
Embed Size (px)
Citation preview
C# Programmer's Handbook Copyright © 2004 by Apress Originally published by Apress in 2004
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 978-1-59059-270-0 ISBN 978-1-4302-0797-9 (eBook) DOI 10.1007/978-1-4302-0797-9
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.
Editorial Board: Dan Appleman, Craig Berry, Gary Cornell, Tony Davis, Steven Rycroft, Julian Skinner, Martin Streicher, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski
Assistant Publisher: Grace Wong
Copy Editor: Marilyn Smith
Production Manager: Kari Brooks
Proofreader: Lori Bring
Compositor: Susan Glinert Stevens
Indexer: Michael Brinkman
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17,69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, email orders@springer-ny. com, or visit http: //www. springer-ny. com. Outside the United States: fax +49 6221345229, email [email protected], or visit ht tp: //www. springer. de.
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, email in f o@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 indirectiy by the information contained in this work.
The source code for this book is available to readers at ht tp: //www. apress. com in the Downloads section. You will need to answer questions pertaining to this book in order to successfully download the code.
Contents at a Glance
About the Author ................................................................................................. xv
Acknowledgments ................................................................................................ xvii
Preface .................................................................................................................. xix
Part One
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7 Chapter 8
Chapter 9
Part Two
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
C# Language Basics ................................................... 1
Common Type System ................................................................. 3
Expressions and Operators ............................................... 29
Structs, Enums, Arrays, and Collections ................. 45
Program Control ..................................................................... 65
Building Your OWn Classes ............................................... 77
Strings ..................................................................................... 105
Memory Management ............................................................... 121
Delegates, Events, and Namespaces ............................ 131
Exception Handling and Application Domains ........ 141
Common Tasks ............................................................. 149
COM and .NET Interoperability ..................................... 151
Files and Streams ............................................................... 193
Active Directory ................................................................. 213
ADO.NET ..................................................................................... 229
Networking and WWW Connections ................................... 259
Threading ................................................................................ 269
Debugging ................................................................................ 289
Remoting ................................................................................... 353
Web Services .......................................................................... 397
Office Integration ............................................................. 407
Windows Forms ........................................................................ 423
v
Contents at a Glance
vi
Part Three ASP. NET .................................................................................... 439
Chapter 21
Chapter 22
Chapter 23
HTML Basics ............................................................................ 441
XML .........................................................................................•.... 465
ASP. NET ..................................................................................... 473
Part Four Appendixes .................................................................. 499
Appendix A
Appendix B
Appendix C
C# Tools and Resources .................................................... 501
Exam 70-315 ............................................................................ 503
Exam 70-316 ............................................................................ 525
Index ...................................................................................................................... 541
Contents
About the Author .......................................................................................... xv
Acknowledgments .......................................................................................... xvii
Preface .............................................................................................................. xix
Part One e# Language Basics .................................................. 1
Chapter 1 Common Type System ........................................................ 3
Identifiers and Naming Conventions .......................................................... 4 Variables ................................................................................................................. 6 Value Types ............................................................................................................. 8 Reference Types .................................................................................................. 20 Constant and Static Values .......................................................................... 23 Scopes ...................................................................................................................... 25 Casting and Class Conversion ...................................................................... 26
Chapter 2 Expressions and Operators .................................... 29
Expressions ........................................................................................................... 30 Arithmetic Operators ....................................................................................... 31 Relational Operators ....................................................................................... 36 Logical Operators .............................................................................................. 37 Bitwise Operators .............................................................................................. 38 Shift Operators .................................................................................................. 40 Precedence Table ................................................................................................ 42
Chapter 3 Structs ~ Enums ~ Arrays ~ and Collections 45
Structs ................................................................................................................... 46 Enums ........................................................................................................................ 48
Arrays ...................................................................................................................... 50 System. Collections ........................................................................................... 53 Stacks ...................................................................................................................... 61 Iterators ............................................................................................................... 64
vii
Contents
Chapter 4 Program Control .............................................................. 65
Selection Statements ....................................................................................... 66 Iterator Statements ......................................................................................... 70 Jump Statements .................................................................................................. 74
Chapter 5 Building Your Own Classes .................................... 77
Object Methods .................................................................................................... 7B Class Declaration and Definition ............................................................. BO Constructors and Initialization ............................................................... B2 Destroying Objects ............................................................................................ B4 Passing by Value and by Reference ........................................................... B7 Class Instances .................................................................................................. 90 Over loading ........................................................................................................... 91 Core OOP Concepts .............................................................................................. 92 Virtual Methods .................................................................................................. 95
Overriding ............................................................................................................. 97 Interfaces ............................................................................................................. 99 Generics ............................................................................................................ ... 102
Partial Types ..................................................................................................... 104
Chapter 6 Strings ................................................................................. 105
String Creation ................................................................................................ 106
String Concatenation ..................................................................................... lOB Substrings ........................................................................................................... 110 Character Manipulation ................................................................................. 112 Joining and Splitting Strings .................................................................. 115 Trimming and Padding Strings .................................................................... 117 String Formatting ............................................................................................ 119
Chapter 7 Memory Management ....................................................... 121
Virtual Memory and Address Space ........................................................... 122 Heap and Stack Memory ................................................................................... 123 References and Pointers ............................................................................... 125 The Garbage Collector (GC) ........................................................................ 127
viii
Contents
Chapter 8 Delegates) Events) and Namespaces .............. 131
Delegates ............................................................................................................. 132 Events .................................................................................................................... 135 Anonymous Methods ............................................................................................ 139 Namespaces and the using Keyword ........................................................... 140
Chapter 9 Exception Handling and Application Domains ........................................ 141
Exception Handling ......................................................................................... 142 The . NET Exception Class ............................................................................ 145 Application Domains ....................................................................................... 146 Fusion Basics .................................................................................................... 148
Part Two Common Tasks ........................................................... 149
Chapter 10 COM and .NET Interoperability ........................ 151
COM Interop Reference ................................................................................... 152 COM Interop Rules and Facts ...................................................................... 159 RCW-Consuming COM Objects: Early Binding ......................................... 162 Consuming COM Objects: Late Binding .................................................... 164 QueryInterface and Casting ........................................................................ 165 CCW: Consuming .NET Objects from COM Clients ................................. 166 Subscribing and Publishing to Events .................................................. 168 Using Platform Invoke to Call Native DLL Functions ................... 171 Creating COM + Objects in . NET ............................................................... 180 Using Role-Based Security in Managed COM+ Applications ........... 182 Advanced Customizing of Structures ...................................................... 184 Using Arrays in COM Interop ...................................................................... 186
Chapter 11 Files and Streams ....................................................... 193
Directory Management ..................................................................................... 194 File Management ................................................................................................ 196 Streams ................................................................................................................. 204
ix
Contents
Chapter 12 Active Directory .......................................................... 213
Accessing Active Directory ........................................................................ 214 Connecting and Binding to Active Directory ..................................... 215 Traversing Objects in Active Directory .............................................. 216 Reading Attributes of an Object ............................................................. 217 Writing New Objects to Active Directory ............................................ 219 Writing and Modifying Attributes of an Object ............................... 220 Changing Passwords .......................................................................................... 222 Deleting Objects from Active Directory .............................................. 223 Searching the Directory ............................................................................... 224 Directory Caching ............................................................................................ 226
Chapter 13 ADO. NET ................................................................................. 229
Basic ADO.NET Concepts ................................................................................. 230 Creating Database Connections .................................................................. 232 Using DataSets .................................................................................................. 234 Using DataReader .............................................................................................. 241 Using ADO.NET Events ..................................................................................... 242 Creating Data Tables and Populating Them .......................................... 243 Working with Data in a Database ............................................................. 245 Using DataViews to Control the Display of a DataGrid ............... 256 ADO.NET Changes from 1.0 to 1.1 of the Framework ........................ 257
Chapter 14 Networking and "''''''' Connections ..................... 259
Socket Connections .......................................................................................... 260 DNS Name Resolution ....................................................................................... 263 Web Clients and Web Requests .................................................................... 264 Displaying Web Pages Using the WebBrowser Control ...................... 266 Networking and WWW Changes from 1.0 to 1.1 of the Framework 267
Chapter 15 Threading ............................................................................ 269
Threading Basics .............................................................................................. 270 Creating Threads .............................................................................................. 271 Destroying Threads .......................................................................................... 273 Using Background Threads ............................................................................. 274 Controlling Thread Execution .................................................................... 275 Synchronizing Threads ................................................................................... 279 Using Thread Pools .......................................................................................... 282 Making Asynchronous Calls .......................................................................... 285
x
Contents
Chapter 16 Debugging ........................................................................... 289
Tracing ................................................................................................................. 290 Creating and Writing to an Event Log .................................................. 297 Measuring Elapsed Time ................................................................................. 298 Using Asserts .................................................................................................... 301
Using Breakpoints ............................................................................................ 302 Power Debugging the CIL .............................................................................. 303
Debugging with Cordbg ................................................................................... 309 Debugging Assembly X386 .............................................................................. 311 Debugging with Windbg ................................................................................... 315 Debugging with SOS Commands ...................................................................... 317 Writing Data to Performance Monitor .................................................... 342 Debugging Memory Leaks ................................................................................. 345 Debugging Contention Problems in ASP. NET ......................................... 347 Enabling Customer Debug Probes for Interop Services ................. 348
Chapter 17 Remoting .............................................................................. 353
Remoting Basics ................................................................................................ 354 Remoting Configuration Files and Configuration Attributes .... 356 Creating a Hosting Process ........................................................................ 360
Creating Remoted Objects ............................................................................ 366 Server Programmatic Design for Remoting ............................................ 368 Server Configuration File Design for Remoting .............................. 371 Client Programmatic Design for Remoting ............................................ 375 Client Configuration File Design for Remoting .............................. 378
Managing Object Lifetimes .......................................................................... 381 Using Client-Side Sponsors ........................................................................ 383 Calling Remoted Methods Asynchronously .............................................. 385 Using Call Context ......................................................................................... 387 Using Sinks ......................................................................................................... 389 Remoting and Threading Issues ................................................................. 391 Using Custom Serialization ........................................................................ 393
Chapter 18 Web Services .................................................................... 397
Web Services Basics ....................................................................................... 398
Creating a Simple Web Service ................................................................. 399 Creating a Web Service Client ................................................................. 400 Golden Rules for Web Services ................................................................. 401
xi
Contents
Chapter 19 Office Integration .................................................... 407
Automating Office Basics ............................................................................. 408 Creating a New Instance of an Office Application ........................ 409 Using the ROT and File Monikers ............................................................. 410 Modifying Office Document Properties .................................................. 412 Creating Command Bars ................................................................................... 413 Using Events in Office ................................................................................. 415 Sending E-Mail .................................................................................................. 417
Chapter 20 Windows Forms ................................................................. 423
Basic Windows Forms ....................................................................................... 424 Common Controls) Properties) and Events ............................................ 427 Alternatives for Creating Windows Forms ............................................ 433 Message Windows ................................................................................................ 434 Windows Form Changes from 1.0 to 1.1 of the Framework ............. 438
Part Three ASP. NET ....................................................................... 439
Chapter 21 HTML Basics ....................................................................... 441
Why HTML in a Book on C#? .......................................................................... 442 Creating a Basic HTML Page ........................................................................ 443 Making Text Look Better ............................................................................... 448 Working with Lists .......................................................................................... 449 Using Tables to Build Pages ...................................................................... 451 Using Controls .................................................................................................. 453 Using Scripts ..................................................................................................... 457 Using Style Sheets (CSS) ............................................................................. 458 HTML Quick Reference ..................................................................................... 462
Chapter 22 XML ........................................................................................... 465
XML Basics ........................................................................................................... 466 Reading XML ......................................................................................................... 467 Writing XML to a File ................................................................................... 470 Loading an XML Document ............................................................................... 472
xii
Contents
Chapter 23 ASP. NET ................................................................................. 473
ASP.NET Basics .................................................................................................. 474
ASP.NET Directives ......................................................................................... 475
Application State ............................................................................................ 477
Common ASP. NET Events ................................................................................... 478
ASP. NET Configuration ................................................................................... 479
User Sessions: Browser Cookies ............................................................... 484
User Sessions: Session State .................................................................... 486
User Sessions: Cookieless .......................................................................... 489
ASP. NET Caching ................................................................................................ 490 Forms-Based Authentication ........................................................................ 493
Windows-Based Authentication .................................................................... 494
ASP. NET Controls .............................................................................................. 495
Part Four Appendixes ................................................................ 499
Appendix A C# Tools and Resources .......................................... 501
Appendix B Exam 70-315 ....................................................................... 503
Creating User Services ................................................................................. 503 Creating and Managing Components in . NET Assemblies ................. 513
Consuming and Manipulating Data ............................................................. 514
Testing and Debugging ................................................................................... 519 Deploying a Web Application ...................................................................... 521 Maintaining and Supporting a Web Application ................................. 522
Configuring and Securing a Web Application ..................................... 523
Appendix C Exam 70-316 ....................................................................... 525
Creating User Services ................................................................................. 525
Creating and Managing Components and . NET Assemblies ............... 528
Consuming and Manipulating Data ............................................................. 530 Testing and Debugging ................................................................................... 534 Deploying a Windows-Based Application ................................................ 536
Maintaining and Supporting a Windows-Based Application ........... 537 Configuring and Securing a Windows-Based Application ............... 538
Index ................................................................................................................... 541
xiii
About the Author
Gregory S. MacBeth is currently employed by Microsoft as a Solution Integration Engineer in Charlotte, North Carolina, where he helps customers realize the potential of Microsoft products. He specifically aids customers in resolving complex problems where multiple products are involved. In addition, he often assists customers in developing first-class software using C# and .NET. He holds MCAD, MCSD (.NET and VS 6), MCSE, MCDBA, MCT, N+, andA+ certifications.
xv
Acknowledgments
1b MY WIFE PAIGE and my sons Welby and Sumner, thanks for your patience and love, and the sacrifices that you made during the time it took me to complete this work.
To my father Mac, Major Allen D. Broussard, Captain Kenneth Poteet, and the United States Marine Corps for making me the man I am today.
To Ken Burns, my former manager at Microsoft, thanks for being understanding, supportive, and helping me to develop my career. But most of all, thanks for being a friend!
Thanks to the staff members at Apress for all their help.
xvii
Preface
I WAS INSPIRED to write this book because when I needed to transition from C++ to
C#, I found few books that helped. All I really wanted was a book that defined the
language and showed me simple examples of how to use the .NET Framework.
I hope that you will find this book helpful and useful in learning C#.
As you read this book, please keep in mind that my goal was to provide you
with simple samples showing you how to use this language to do common tasks.
I stripped out all but the essential code. Specifically, I removed safeguards such as
exception handling that would normally be present in all good code. I advise you
strongly to use exception handling in your own code.
I have also included in this book material that will be in the next major release
of C#, such as generics, anonymous methods, iterators, and more.
I wish you the best of luck in your coding.
xix