Chapter 9. Arrays, Indexers, and Collections

The .NET Framework provides a rich suite of collection classes, including Array, ArrayList, NameValueCollection, StringCollection, Queue, Stack, and BitArray.

The simplest collection is the Array, the only collection type for which C# provides built-in support. In this chapter you will learn to work with single, multidimensional, and jagged arrays. You will also be introduced to indexers, a bit of C# syntactic sugar that makes it easier to access class properties, as though the class were indexed like an array.

The .NET Framework provides a number of interfaces, such as IEnumerable and ICollection, whose implementation provides you with standard ways to interact with collections. In this chapter you will see how to work with the most essential of these. The chapter concludes with a tour of commonly used .NET collections, including ArrayList, Dictionary, Hashtable, Queue, and Stack.

Arrays

An array is an indexed collection of objects, all of the same type. C# arrays are somewhat different from arrays in C++ and other languages—because they are objects. This provides them with useful methods and properties.

C# provides native syntax for the declaration of Array objects. What is actually created, however, is an object of type System.Array . Arrays in C# thus provide you with the best of both worlds: easy-to-use C-style syntax underpinned with an actual class definition so that instances of an array have access to the methods and properties ...

Get Programming C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.