Friday 25 July 2014

Integer Extension

When using Linq & Lambda expressions I find it easier to write a word based approach than writing long hand. Take for example this query
var something = (from myObject in myTable where myObject.Number > 1 && myObject.Number < 10 select myObject);
We could simply make this code cleaner and easier to read by writing
var something = (from myObject in myTable where myObject.Number.IsWithin(1,10) select myObject);
And the brains behind this method is this little extension method.
namespace FieldOfSheep
{
public static class IntegerExtensions
    {
        public static bool IsWithin(this int value, int minimum, int maximum)
        {
            return value >= minimum && value <= maximum;
        }
    }
}

No comments:

Post a Comment