Language Envy - C# needs Ranges
Posted by Sergio
on 2010-01-02
As soon as I started learning Ruby, a few years ago, I got immediately hooked
on its Range
class.
I could not believe I had been programming in .NET without them for so long.
I like to think of range objects as the specification for a for
loop, packaged in an object that can be passed around. That's not the whole story, though.
Ranges also represent sequences or intervals, which can be queried for intersection or
containment. See the following Ruby sample code.
#declare a Range object summer_months = 6..9 #enumerate it summer_months.each {|m| puts "#{Date.new(2000, m, 1).strftime('%B')} is a Summer month." } #other handy features summer_months.include? 7 # ==> true summer_months.to_a # ==> [6, 7, 8, 9] (converted to array)
I needed that in C#
That was back when the CLR 2.0 was just about to be released and I ended up writing my own Range