Chapter 12 Classes and Structures
What’s in This Chapter
- Defining classes and structures
- Value and reference types
- Memory requirements, and heap and stack performance
- Boxing and unboxing
- Constructors and destructors
- Garbage collection and
Dispose
Wrox.com Downloads for This Chapter
Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/csharp5programmersref on the Download Code tab.
A variable holds a single value. It may be a simple value, such as an int
or string
, or a reference to a more complex entity such as a class or structure.
Classes and structures are both container types. They group related data values and methods into a convenient package that you can manipulate as a group.
For example, a Recipe
class would contain fields or properties holding information about a recipe such as ingredients, temperature, instructions, and number of servings. It could also include methods to convert measurements between English and metric, to scale the recipe for different numbers of servings, and to print the recipe.
If you make an instance of the Recipe
class and fill it with the data for a particular recipe, you can move the object around as a single unit instead of passing around separate variables holding the ingredients, temperature, instructions, and number of servings. The object also contains methods for manipulating the recipe, so you don’t need to write a separate module ...
Get C# 5.0 Programmer's Reference 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.