Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Arrays and Collections

Last updated Nov 7, 2003.

One of the most common programming tasks is working with groups of related data: employee lists, collections of parts, itemized banking transactions, and so on. Most mainstream programming languages have an array construct that lets you group the related data into a single structure so that you can manipulate it more easily. Over the years, language runtime libraries have grown to include more capable and specialized constructs, such as sorted lists, dynamic arrays, and dictionary classes, allowing you to store and access items by key in addition to the position in the array. (Two examples are the C++ Standard Template Library and Delphi's Visual Control Library.)

The .NET Framework provides many different collection classes, from the generic Array to the very specialized NameValueCollection. These classes include:

Array is a fixed-capacity construct that can have one or more dimensions. The .NET Framework supports multi-dimensional arrays similar to Pascal arrays, and jagged arrays such as you find in C.

ArrayList is a one-dimensional, expandable list that provides some advanced capabilities like the ability to add, insert, or remove multiple items in a single operation.

Hashtable is a collection in which each entry consists of a name/value pair. Hashtable allows for quick retrieval of an item based on its hash key.

SortedList is something of a cross between a Hashtable and an ArrayList. The items in a SortedList can be accessed by index, by key, or by value.

Queue and Stack implement the common queue and stack data structures. A Queue is a first-in-first-out construct that implements three primary operations: Enqueue, Dequeue, and Peek. Stack, like the stack in a processor, implements Push, Pop, and Peek. These two collections are very useful for temporary storage, and for implementing common algorithms that use those data structures.

Bit collections provide efficient storage of and access to collections of single-bit flags. Methods allow you to apply Boolean filters to ranges of flags.

In addition to these and other built-in collection types, the .NET Framework provides base classes (CollectionBase and NameValueCollectionBase, for example) that allow you to define your own generic or strongly-typed collections.

Using Collections

When deciding which collection type to use in your application, you need to consider many factors. Choosing the wrong collection type will hamper your ability to access data, and can cause severe performance problems. Using an Array, for example, when you need to access items by key or maintain items in sorted order, will cause you to write a lot of extraneous code that searches or sorts the items. The Grouping Data in Collections topic in MSDN has some good guidelines for selecting a collection class. In this section, I will provide examples of using some of the Framework-supplied collection classes.

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network