Wiki

Clone wiki

Unit Class Library / Overview

Unit Class Philosophy

Let’s look at an example of a simple unit: Distance - This class describes distances. Things like feet, kilometers and miles. The reason for writing these as classes instead of just using doubles like other implementations is so that we can forget about units. For example, How long is this line: ------------ ? You could describe the length in inches, millimeters, or any other unit of distance which will all have different numerical values, yet still describe the same length of line. The line itself is unit agnostic. It is still the same length no matter how it is described. Therefore, we decided to make classes that could handle describing the line in a variety of manners, while hiding the math and comparison logic of converting between different unit types. Also, to do all of this while still maintaining the highest accuracy possible with this functionality.

To use these classes, you must also have a way of describing which units you are using to describe the initial number used to create the unit object. This is fulfilled by enumerated sets. Each unit type has an enumerated set titled: (the units name + “Type”) e.g DistanceType.Inch or AngleType.Radian.

Using Unit Class Library

Making a Unit is super easy, Just pass in a value and an Enum explaining what that value means: For example, I could create a Distance class like this

Distance myDistance = new Distance(DistanceType.Millimeter, 1000);

and then use it as Meters like this

#!c#
myDistance.Meters; // would equal 1

We can do everything with them that we would have with doubles:

  • Math operations:
    #!c#
    Distance ourDistance = (myDistance + yourDistance) - theirDistance;
    
  • Comparisons

    #!c#
    bool mineIsBigger = myDistance > yourDistance;
    

  • Sorting

    #!c#
    List<Distance> sortedDistance = ListOfDistance.Sort();
    

  • User defined equality, for the times when you know the accuracy and precision of your input values.

    #!c#
    Distance distance1  = new Distance(DistanceType.Millimeter, 1000500);
    Distance distance2  = new Distance(DistanceType.Kilometer, 1);
    
    Distance acceptableDeviation = new Distance(DistanceType.Meter, .5);
    bool areCloseEnoughToBeConsideredEqual = distance1.EqualsWithinPassedAcceptedDeviation(distance2, acceptableDeviation);
    
    There are some things that the library intentionally does not do in the way that you might expect:

  • Incrementing/Decrementing

#!c#
Distance oneMoreOf = myDistance++; //compile error
Distance oneLessOf = myDistance--; //compile error
Because the unit objects are supposed to be thought of as simultaneously existing as all unit types, it does not make sense to increment or decrement.

  • ToString()
    #!c#
    Distance myDistance= new Distance(DistanceType.Kilometer, 1); 
    string distanceString = myDistance.ToString(); // "1 Kilometer"
    
    The unit objects are supposed to be thought of as simultaneously existing as all unit types, but from a practical standpoint it is nice to see a Distance object's value expressed in terms the user defined.

If there is any functionality not listed here that you think would be useful to add, please leave us a suggestion on our issue tracker. Or better yet, add the functionality yourself by following our guide for Obtaining and Submitting Code for Contribution

Updated