www.it-ebooks.info www.it-ebooks.info Developer’s Guide to Collections in Microsoft ® .NET Calvin Janes www.it-ebooks.info Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, California 95472 Copyright © 2011 by Calvin Janes 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. ISBN: 978-0-7356-5927-8 1 2 3 4 5 6 7 8 9 LSI 6 5 4 3 2 1 Printed and bound in the United States of America. Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/ IntellectualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are ctitious. 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 without any express, statutory, or implied warranties. Neither the authors, O’Reilly Media, Inc., 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 and Developmental Editor: Russell Jones Production Editor: Holly Bauer Editorial Production: Online Training Solutions, Inc. Technical Reviewer: Chris G. Williams Copyeditor: Jaime Odell, Online Training Solutions, Inc. Proofreader: Victoria Thulman, Online Training Solutions, Inc. Indexer: Ron Strauss Cover Design: Twist Creative • Seattle Cover Composition: Karen Montgomery Illustrator: Jeanne Craver, Online Training Solutions, Inc. www.it-ebooks.info iii Contents at a Glance Part I Collection Basics 1 Understanding Collections: Arrays and Linked Lists . . . . . . . . . . . 3 2 Understanding Collections: Associative Arrays . . . . . . . . . . . . . . 87 3 Understanding Collections: Queues, Stacks, and Circular Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Part II .NET Built-in Collections 4 Generic Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 5 Generic and Support Collections . . . . . . . . . . . . . . . . . . . . . . . . . 283 Part III Using Collections 6 .NET Collection Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 7 Introduction to LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 8 Using Threads with Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . 469 9 Serializing Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Part IV Using Collections with UI Controls 10 Using Collections with Windows Form Controls . . . . . . . . . . . . 539 11 Using Collections with WPF and Silverlight Controls . . . . . . . . 583 www.it-ebooks.info www.it-ebooks.info v Table of Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii Part I Collection Basics 1 Understanding Collections: Arrays and Linked Lists . . . . . . . . . . . 3 Array Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Uses of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Advantages of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Disadvantages of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Array Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Understanding Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Creating Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Allowing Users to Add Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Allowing Users to Remove Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Adding Helper Methods and Properties . . . . . . . . . . . . . . . . . . . . . . . . 17 Using the ArrayEx(T) Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Linked List Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Uses of Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Advantages of Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Disadvantages of Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Linked List Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Singly Linked List Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Doubly Linked List Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Using an Array to Create a Linked List. . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Using the Linked List Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey www.it-ebooks.info vi Table of Contents 2 Understanding Collections: Associative Arrays . . . . . . . . . . . . . . 87 Associative Array Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Uses of Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Advantages and Disadvantages of Associative Arrays . . . . . . . . . . . . . 88 Associative Array Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Using Association Lists for Associative Arrays . . . . . . . . . . . . . . . . . . . . 88 Using Hash Tables for Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . 105 Using the Associative Array Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 3 Understanding Collections: Queues, Stacks, and Circular Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Queue Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Uses of Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Queue Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Using an Array to Implement a Queue . . . . . . . . . . . . . . . . . . . . . . . . . 154 Using a Linked List to Implement a Queue. . . . . . . . . . . . . . . . . . . . . . 163 Using the Queue Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Stack Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Uses of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Stack Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Adding the Common Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Using an Array to Implement a Stack . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Using a Linked List to Implement a Stack. . . . . . . . . . . . . . . . . . . . . . . 182 Using the Stack Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Circular Buffer Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Uses of Circular Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Circular Buffer Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Understanding the Internal Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Creating Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Allowing Users to Add Items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Allowing Users to Remove Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Adding Helper Methods and Properties . . . . . . . . . . . . . . . . . . . . . . . 205 Changing Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Using the CircularBuffer(T) Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 www.it-ebooks.info Table of Contents vii Part II .NET Built-in Collections 4 Generic Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Understanding the Equality and Ordering Comparers. . . . . . . . . . . . . . . . . 215 Understanding the Equality Comparer . . . . . . . . . . . . . . . . . . . . . . . . . 215 Understanding the Ordering Comparer . . . . . . . . . . . . . . . . . . . . . . . . 218 Understanding Delegates, Anonymous Methods, and Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Lambda Expressions in Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 List(T) Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Using the List(T) Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Creating a List(T) 222 Appending Items to a List(T) 223 Traversing a List(T) 224 Removing Items from a List(T) 228 Inserting Items into a List(T) 230 Sorting a List(T) 231 Searching a List(T) 247 Checking the Contents of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Modifying the List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 LinkedList(T) Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Using the LinkedList(T) Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Creating a New LinkedList(T) 273 Adding to the LinkedList(T) 273 Removing Nodes from the LinkedList(T) 277 Obtaining Information on the LinkedList(T) 279 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 5 Generic and Support Collections . . . . . . . . . . . . . . . . . . . . . . . . . 283 Queue(T) Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Using the Queue(T) Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Creating a Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Adding Items to the Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Removing Items from the Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Checking the Queue. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Cleaning the Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Stack(T) Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 www.it-ebooks.info viii Table of Contents Using the Stack(T) Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Creating a Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Adding Items to the Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Removing Items from the Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Checking the Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Cleaning the Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Dictionary(TKey,TValue) Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Understanding Dictionary(TKey,TValue) Implementation . . . . . . . . . . . . . . 293 Using the Dictionary(TKey,TValue) Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Creating a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Adding Items to a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Removing Items from a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 Retrieving Values from the Dictionary by Using a Key. . . . . . . . . . . . 299 Checking the Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 BitArray Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Using the BitArray Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Creating a BitArray 306 Accessing Bits in the BitArray Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Using the BitArray Class for Bit Operations . . . . . . . . . . . . . . . . . . . . . 310 CollectionBase and DictionaryBase Overview . . . . . . . . . . . . . . . . . . . . . . . . 316 Using CollectionBase 317 Using DictionaryBase 324 HashSet(T) Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Using the HashSet(T) Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Creating a HashSet(T) 329 Adding Items to the HashSet(T) 330 Removing Items from a HashSet(T) 331 Performing Set Operations on a HashSet(T) 333 Sorted Collections Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 SortedList(TKey, TValue). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 SortedDictionary(TKey, TValue) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 www.it-ebooks.info [...]... and 11, which show you how to use collections with simple data binding in Windows Forms, Silverlight, and Windows Presentation Foundation (WPF) Finding Your Best Starting Point in This Book Developer’s Guide to Collections in Microsoft NET is designed to be a complete guide to collections Depending on your needs and your existing understanding of collections, you may want to focus on specific areas... with collections in Part III, and be sure to read Chapter 7 in Part III Interested in integrating your collection classes with the NET Framework Focus on Parts I and III if you need help creating collection classes and integrating them with the NET Framework Interested in using LINQ with collections Read through Chapter 7 in Part III Interested in using threads with collections Read through Chapter 8 in. .. Part IV Using Collections with UI Controls 10 Using Collections with Windows Form Controls 539 Simple Binding 539 Two-Way Data Binding 540 Implementing the IBindingList Interface 541 Implementing the IBindingListView Interface 562 Using the BindingList(T)... www.it-ebooks.info Introduction xv In Chapter 9, you learn how to serialize collections and how to add serialization support to your custom collection classes You also learn how to control what gets serialized in your custom collection classes Part IV, Using Collections with UI Controls At some point in your development career, you will find a need to display your collection to the end user Part IV includes... Chapter 8 in Part III Interested in serializing your custom collections Read through Chapter 9 in Part III Interested in using collections in your user interface (UI) Read through the chapters in Part IV Most of the book’s chapters include hands-on samples that you can use to try out the concepts just explained No matter which sections you choose to focus on, be sure to download and install the sample... http://twitter.com/MicrosoftPress www.it-ebooks.info www.it-ebooks.info Developer’s Guide to Collections in Microsoft NET Part I Collection Basics www.it-ebooks.info 1 www.it-ebooks.info Chapter 1 Understanding Collections: Arrays and Linked Lists After completing this chapter, you will be able to ■ Identify arrays and singly and doubly linked lists ■ Design and implement arrays and singly and doubly linked lists ■ Understand... As T In items Add(item) Next End Sub Each item in items will be added to the end of the array in the order it was passed in Allowing Users to Add Items The array would be useless if you could never add anything to it To add items, you need to add methods for both adding and inserting items into the array This can be accomplished through the Add and Remove methods You can use the following method to add... source code respectively ■ All custom collection classes are present in the DevGuideToCollections project in each chapter folder under CS for C# or VB for Visual Basic NET DevGuideToCollections is a class library that you can use in your own project To view the source code, access the Developer’s Guide to Collections in the main folder If your system is configured to display file extensions, the system... Using the BindingSource Class 575 Understanding the Sample Code 576 Binding with the ComboBox Control 576 Binding with the ListBox Control 577 Binding with the DataGridView Control and IBindingList 578 Binding with the DataGridView Control and IBindingListView 580 Binding... such issues matter Microsoft has a class with similar functionality called ArrayList This class is named ArrayEx(T) to eliminate any possible confusion with the built -in ArrayList class You can find the fully implemented code in the DevGuideToCollections project, in either the Chapter 1\CS\DevGuideToCollections or Chapter 1\VB\DevGuideToCollections folder Note The ArrayList class in the NET Framework . (WPF). Finding Your Best Starting Point in This Book Developer’s Guide to Collections in Microsoft .NET is designed to be a complete guide to col- lections. Depending on your needs and your existing. using LINQ with collections Read through Chapter 7 in Part III. Interested in using threads with collections Read through Chapter 8 in Part III. Interested in serializing your custom collections. www.it-ebooks.info www.it-ebooks.info Developer’s Guide to Collections in Microsoft ® .NET Calvin Janes www.it-ebooks.info Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005