Chapter 6. Lining Up Your Ducks with Collections

In This Chapter

  • Creating variables that contain multiple items of data: Arrays

  • Going arrays one better with flexible "collections"

  • New features: Array and collection initializers and set-type collections

Simple one-value variables of the sort you may encounter in this book fall a bit short in dealing with lots of items of the same kind: ten ducks instead of just one, for example. C# fills the gap with two kinds of variables that store multiple items, generally called collections. The two species of collection are the array and the more general purpose collection class. Usually, if I mean array, I say so, and if I mean collection class, I just call it that. If I refer to a collection or a list, I usually mean that it can be either one.

An array is a data type that holds a list of items, all of which must be of the same type: all int or all double, for example.

C# gives you quite a collection of collection classes, and they come in various shapes, such as flexible lists (like strings of beads), queues (like the line to buy your Spider-Man XII tickets), stacks (like the semistack of junk on someone's desk), and more. Most collection classes are like arrays in that they can hold just apples or just oranges. But C# also gives you a few collection classes that can hold both apples and oranges at a time — which is useful only rarely. (And you have much better ways to manage the feat than using these elderly collections.)

For now, if you can master ...

Get C# 2010 All-in-One For Dummies® 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.