A Better Enumerable.Range
Personally, I don't enjoy this all that much: Enumerable.Range(0, GetTotalEmployees())
, at least when compared to doing something more like this:
(0).To(TotalEmployees)
.Where(a => a % 2 == 0)
.ToList()
// Output:
// 0, 2, 4, ... , 6, 8... 2n
Simply by adding this extension somewhere:
public static IEnumerable<int> To(this int from, int to)
{
for (int i = from; i < to; i++)
{
yield return i;
}
}
Admittedly it could be made more robust
- Check that
from
is less thanto
. - Add Optional parameter for step.
- Type parameterizing based on the Type of step or of the
to
parameter so that it can produce ints or doubles.
But for the most part this works well.
Written by Lucas Caballero
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#