Chapter 8. Enums and Collections: Organizing your Data
Data isn’t always as neat and tidy as you’d like it to be.
In the real world, you don’t receive your data in tidy 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 enums and collections come in. Enums are types that let you define valid values to categorize your data. Collections are special objects that store many values, letting you store, sort, and manage all the data that your programs need to pore through. That way, you can spend your time thinking 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
We’re going to be working with playing cards over the next few chapters, so let’s build a Card class that we’ll use. First, create a new Card class that has a constructor that lets you pass it a suit and value, which it stores as strings:
class Card { public string Value { get; set; } public string Suit { get; set; } public string Name { get { return $"{Value} of {Suit}"; } } public Card(string value, string suit) { Value = value; Suit = suit; } }
That looks pretty good. We can create ...
Get Head First C#, 4th 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.