Skip to Content
Head First C#, 3rd Edition
book

Head First C#, 3rd Edition

by Jennifer Greene, Andrew Stellman
September 2013
Beginner content levelBeginner
1098 pages
31h 24m
English
O'Reilly Media, Inc.
Content preview from Head First C#, 3rd Edition

Chapter 8. Enums and Collections: Storing lots of data

image with no caption

When it rains, it pours.

In the real world, you don’t get to handle your data in tiny little bits and pieces. No, your data’s going to come at you in loads, piles, and bunches. You’ll need some pretty powerful tools to organize all of it, and that’s where collections come in. They let you store, sort, and manage all the data that your programs need to pore through. That way, you can think about writing programs to work with your data, and let the collections worry about keeping track of it for you.

Strings don’t always work for storing categories of data

Suppose you have several worker bees, all represented by Worker classes. How would you write a constructor that took a job as a parameter? If you use a string for the job name, you might end up with code that looks like this:

image with no caption

You could probably add code to the Worker constructor to check each string and make sure it’s a valid bee job. However, if you add new jobs that bees can do, you’ve got to change this code and recompile the Worker class. That’s a pretty short-sighted solution. What if you have other classes that need to check for the types of worker bees they can be? Now you’ve got to duplicate code, and that’s a bad path to go down.

What we need is a way to say, “Hey, there are ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene
Head First C#, 2nd Edition

Head First C#, 2nd Edition

Andrew Stellman, Jennifer Greene
Head First C#, 5th Edition

Head First C#, 5th Edition

Andrew Stellman, Jennifer Greene
Head First C

Head First C

David Griffiths, Dawn Griffiths

Publisher Resources

ISBN: 9781449358846Errata PageSupplemental Content