Microsoft Visual C /CLI Step By Step

Transcription

Microsoft Visual C /CLIStep by StepJulian Templeman

Copyright 2013 by Julian TemplemanAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by anymeans without the written permission of the publisher.ISBN: 978-0-7356-7517-91 2 3 4 5 6 7 8 9 LSI 8 7 6 5 4 3Printed and bound in the United States of America.Microsoft Press books are available through booksellers and distributors worldwide. If you need support relatedto this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think ofthis book at soft and the trademarks listed at ctualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property oftheir respective owners.The example companies, organizations, products, domain names, email addresses, logos, people, places, andevents depicted herein are fictitious. No association with any real company, organization, product, domain name,email 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 withoutany 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 directlyor indirectly by this book.Acquisitions and Developmental Editor: Russell JonesProduction Editor: Kara EbrahimTechnical Reviewer: Luca RegnicoliCopyeditor: Octal Publishing, Inc.Indexer: BIM Indexing and Proofreading ServicesCover Design: Twist Creative SeattleCover Composition: Ellie VolckhausenIllustrator: Rebecca Demarest

I would like to dedicate this book to my wife, Jane, withoutwhose steadfast love and support none of this would be possible.—Julian Templeman

Contents at a GlanceIntroductionxxiPart IGETTING STARTED WITH C .NETChapter 1Hello C !Chapter 2Introducing object-oriented programming13Chapter 3Variables and operators23Chapter 4Using functions37Chapter 5Decision and loop statements57Chapter 6More about classes and objects77Chapter 7Controlling object lifetimes103Chapter 8Inheritance121Part IIMICROSOFT .NET PROGRAMMING BASICSChapter 9Value types143Chapter 10Operator overloading159Chapter 11Exception handling175Chapter 12Arrays and collections197Chapter 13Properties229Chapter 14Delegates and events245Chapter 15The .NET Framework class library263Part IIIUSING THE .NET FRAMEWORKChapter 16Working with files281Chapter 17Reading and writing XML305Chapter 18Using ADO.NET333Chapter 19Writing a service by using WindowsCommunication Foundation351Chapter 20Introducing Windows Store apps369Chapter 21More about Windows Store apps3973

viPart IVADVANCED TOPICSChapter 22Working with unmanaged code437Chapter 23Attributes and reflection453Chapter 24Living with COM475Index487Contents at a Glance

ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiPart IGETTING STARTED WITH C .NETChapter 1Hello C !3What is C /CLI?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Your first C /CLI application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The main function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4C keywords and identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Creating an executable application—theory. . . . . . . . . . . . . . . . . . . . . . . . . . 6Editing the application source files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Compiling the source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Running and testing the application . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Creating an executable application—practice. . . . . . . . . . . . . . . . . . . . . . . . . 7Creating a project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Editing the C source code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Building the executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Executing the application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Chapter 2Introducing object-oriented programming13What is object-oriented programming?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Features of object-oriented programming languages. . . . . . . . . . . . . . . . . 14Encapsulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Classes and objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16vii

Benefits to the development life cycle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16A simple example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Chapter 3Variables and operators23What is a variable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23The fundamental data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Declaring a variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Variable naming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Declaring multiple variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Assigning values to variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Handles and pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Typedefs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29The .NET Framework String class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Operators and expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Assignment operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Arithmetic operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Relational and logical operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Bitwise operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32The ternary operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Type casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Operator precedence and associativity. . . . . . . . . . . . . . . . . . . . . . . . 34Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Chapter 4Using functions37Declaring function prototypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Declaring a simple function prototype . . . . . . . . . . . . . . . . . . . . . . . . 38Declaring parameters in a function prototype. . . . . . . . . . . . . . . . . . 39Declaring the return type in a function prototype . . . . . . . . . . . . . . 39Declaring default values for function parameters. . . . . . . . . . . . . . . 40viiiContents

Defining function bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Calling functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Stepping through the application by using debugger. . . . . . . . . . . 47Understanding local and global scope . . . . . . . . . . . . . . . . . . . . . . . . 51Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Chapter 5Decision and loop statements57Making decisions by using the if statement. . . . . . . . . . . . . . . . . . . . . . . . . . 57Performing one-way tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Performing two-way tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Performing multiway tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Performing nested tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Making decisions by using the switch Statement. . . . . . . . . . . . . . . . . . . . . 65Defining simple switch statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Using fall-through in a switch statement. . . . . . . . . . . . . . . . . . . . . . . 67Performing loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Using while loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Using for loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Using do-while loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Performing unconditional jumps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Chapter 6More about classes and objects77Organizing classes into header files and source files. . . . . . . . . . . . . . . . . . 78Declaring a class in a header file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Implementing a class in a source file. . . . . . . . . . . . . . . . . . . . . . . . . . 81Creating objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Initializing objects by using constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Defining constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Member initialization lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Defining class-wide members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Defining class-wide data members. . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Defining class-wide member functions. . . . . . . . . . . . . . . . . . . . . . . . 90Class constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Contentsix

Using constants in classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Using class-wide constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Using instance constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Defining object relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Defining the LoyaltyScheme Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Implementing the LoyaltyScheme class. . . . . . . . . . . . . . . . . . . . . . . . 96Creating and using LoyaltyScheme objects. . . . . . . . . . . . . . . . . . . . . 97Testing the application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Chapter 7Controlling object lifetimes103The .NET approach to object lifetimes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Destruction and finalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Destructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Finalizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Implementing the destructor and finalizer for a class. . . . . . . . . . . 107Objects and stack semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Copy constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113Relating objects with stack semantics . . . . . . . . . . . . . . . . . . . . . . . . 116Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Chapter 8Inheritance121What is inheritance?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Inheritance terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Inheritance and code reuse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Designing an inheritance hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123A word on substitutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Defining a base class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Defining a derived class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126Creating derived class objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Concrete and abstract classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Overriding member functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131xContents

Protected access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Defining sealed classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Abstract and sealed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Defining and using interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Part IIMICROSOFT .NET PROGRAMMING BASICSChapter 9Value types143Reference types and value types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143The need for value types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Properties of value types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146Creating and using a simple struct. . . . . . . . . . . . . . . . . . . . . . . . . . . 146Investigating the structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147The differences between structures and classes . . . . . . . . . . . . . . . 149Implementing constructors for a structure. . . . . . . . . . . . . . . . . . . . 149Using one structure within another . . . . . . . . . . . . . . . . . . . . . . . . . . 150Copying structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Enumerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Creating and using an enumeration. . . . . . . . . . . . . . . . . . . . . . . . . . 153Using enumerations in applications. . . . . . . . . . . . . . . . . . . . . . . . . . 155Using memory efficiently. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Chapter 10 Operator overloading159What is operator overloading?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159What types need overloaded operators? . . . . . . . . . . . . . . . . . . . . . 160What can you overload?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160Rules of overloading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Overloading operators in managed types. . . . . . . . . . . . . . . . . . . . . . . . . . 161Overloading arithmetic operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Using static operator overloads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Contentsxi

What functions can you overload?. . . . . . . . . . . . . . . . . . . . . . . . . . . 166Implementing logical operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Implementing increment and decrement. . . . . . . . . . . . . . . . . . . . . 171Operators and reference types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Guidelines for providing overloaded operators. . . . . . . . . . . . . . . . 173Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174Chapter 11 Exception handling175What are exceptions?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175How do exceptions work?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Exception types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Throwing exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Handling exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180Using the try and catch construct. . . . . . . . . . . . . . . . . . . . . . . . . . . . 180Customizing exception handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Using the exception hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Using exceptions with constructors . . . . . . . . . . . . . . . . . . . . . . . . . . 184Nesting and rethrowing exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . 185The finally block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188The catch( ) block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Creating your own exception types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Using safe cast for dynamic casting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191Using exceptions across languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Chapter 12 Arrays and collections197Native C arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197Passing arrays to functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Initializing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202Multidimensional arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202Dynamic allocation and arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Generic types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Managed arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207xiiContents

The .NET array class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Basic operations on arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213More advanced array operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Using enumerators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218Other .NET collection classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219The List T class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219The SortedList K,V class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Generics and templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224The STL/CLR library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Chapter 13 Properties229What are properties?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229The two kinds of properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Implementing scalar properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Errors in properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232Auto-implemented properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233Read-only and write-only properties. . . . . . . . . . . . . . . . . . . . . . . . . 233Properties, inheritance, and interfaces. . . . . . . . . . . . . . . . . . . . . . . . 235Implementing indexed properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236The Bank example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236Creating Account class properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . 239Adding accounts to the Bank class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240Implementing the Add and Remove methods . . . . . . . . . . . . . . . . . 240Implementing an indexed property to retrieve accounts. . . . . . . . 241Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244Chapter 14 Delegates and events245What are delegates?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245What is the purpose of delegates?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246Defining delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247Implementing delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247Contentsxiii

What are events?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Implementing an event source class. . . . . . . . . . . . . . . . . . . . . . . . . . 254Implementing an event receiver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256Hooking it all together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262Chapter 15 The .NET Framework class library263What is the .NET Framework?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263The Common Language Runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . 264The Microsoft Intermediate Language. . . . . . . . . . . . . . . . . . . . . . . . 264The Common Type System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264The Common Language Specification. . . . . . . . . . . . . . . . . . . . . . . . 265The .NET Framework class library. . . . . . . . . . . . . . . . . . . . . . . . . . . . 265Assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266The .NET Framework namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268Using namespaces in C applications. . . . . . . . . . . . . . . . . . . . . . . 270The System namespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270The Collections namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272The Collections interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273The Diagnostics namespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274The IO namespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274The Windows namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275The Net namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275The ServiceModel namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275The Xml namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276The Data namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276The Web namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278xivContents

Part IIIUSING THE .NET FRAMEWORKChapter 16 Working with files281The System::IO namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282Implementing text I/O by using readers and writers. . . . . . . . . . . . . . . . . 283Using TextWriter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283The FileStream class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286Using TextReader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287Working with files and directories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Getting information about files and directories. . . . . . . . . . . . . . . . 290Binary I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298The BinaryWriter class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298The BinaryReader class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303Chapter 17 Reading and writing XML305XML and .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305The .NET XML namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306The XML processing classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306Parsing XML by using XmlReader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307Parsing XML with validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315Writing XML by using XmlTextWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Using XmlDocument. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322What is the W3C DOM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323The XmlDocument class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323The XmlNode class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332Chapter 18 Using ADO.NET333What is ADO.NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334ADO.NET data providers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334ADO.NET namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335ADO.NET assemblies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336Contentsxv

Creating a connected application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336Connecting to a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337Creating and executing a command. . . . . . . . . . . . . . . . . . . . . . . . . .340Executing a command that modifies data. . . . . . . . . . . . . . . . . . . . . 341Executing queries and processing the results. . . . . . . . . . . . . . . . . . 342Creating a disconnected application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Disconnected operation using a DataSet. . . . . . . . . . . . . . . . . . . . . . . . . . . 345Quick reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350Chapter 19 Writing a service by using Windows CommunicationFoundation351What is Windows Communication Foundation?. . . . . . . . . . . . . . . . . . . . . 351Distributed systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352Connectivity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353The ABCs of WCF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353Endpoints. . . . . . . . . . . . . . . . .

Contents at a Glance Introductionxxi PART I GETTING STARTED WITH C .NET ChapTer 1 hello C ! 3 ChapTer 2 Introducing object-oriented programming 13 ChapTer 3 Variables and operators 23 ChapTer 4 Using functions 37 ChapTer 5 Decision and loop statements 57 ChapTer 6 More about classes and objects 77 ChapTer 7 Controlling object lifetimes 103 ChapTer 8 Cited by: 2Page Count: 82File Size: 2MB