C# in Depth

Cover of C# in Depth
Order now (3rd edition)

Ages vs dates of birth

Chapter 8: Cutting fluff with a smart compiler: 8.3.4

Created: 3/3/2008
Last updated: 3/3/2008

In a few examples, I have used Age as a property of a Person - a property which is writable. In real code, this is bad idea. The moment after an age has been set, it is inaccurate - whereas a date of birth lasts forever. The age can always be calculated as a read-only property. This also allows the type to be (potentially) immutable.

I used age in the examples as a simple number which could easily be attached to a person - but this shouldn't be copied in real code. In truth, I suspect that very little code shown in books should be copied verbatim. The kind of code which is useful for teaching purposes often highlights one area at the expense of others, partly through the need for brevity in print.