C# - 22 - Making values meaningful

[C# 4.0]

Here's something I found over on CodeProject. Consider the following code:

This looks fine until you realise that there is an unwritten assumption here that the current temperature and the max temperature are in the same units (ie. both celcius or both fahrenheit).

A safer way is the define the temperature values as intrinsically one or the other. This can be accomplished using structs like so:

Then the original code can be re-written:

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License