Lesson 16

Using Arrays and Collections

Each of the data types described in previous lessons holds a single piece of data. A variable might hold an integer, string, or point in time.

Sometimes it's convenient to work with a group of related values all at once. For example, suppose you're the CEO of a huge company that just posted huge gains. In that case, you might want to give each hourly employee a certificate of appreciation and give each executive a 15 percent bonus.

In cases like this, it would be handy to be able to store all of the hourly employees' data in one variable so you could easily work with it. Similarly you might like to store the executives' data in a second variable so it's easy to manage.

In this lesson, you learn how to make variables that can hold more than one piece of data. You learn how to make arrays and different kinds of collections such as a List, Dictionary, Stack, and Queue.

This lesson explains how to build these objects and add and remove items from them. Lesson 19 explains how to get the full benefit of them by looping through them to perform some action on each of the items they contain.

Arrays

An array is a group of values that all have the same data type and that all share the same name. To pick a particular item in the array, the program uses an index, which is an integer greater than or equal to 0.

An array is similar to the mailboxes in an apartment building. The building has a single bank of mailboxes that all have the same street address ...

Get C# 24-Hour Trainer, 2nd Edition 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.