-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
I suggest a feature in C# to easily define ranges. It's a replacement for Enumerage.Range except it defines start and stop points rather than start and count.
Sometime you wish to create a range to perhaps .Select from. I propose the following syntax:
'[' expression '..' expression ']'
The requirements of the type from expression is that both have to be of the same type. You cannot create a range between float and integer for example. The result set is [a → b].
edit: changed to inclusive. F# is inclusive, so why should C# be different?
The type of expression also must support an appropriate incremental operator, prefix ++, postfix++ or += (int), or += T if expression is implicitly convertible from int, in that order.
If the order is reversed (high to low) the result of the expression is an empty set.
Example:
foreach(var item in [1 .. 100])
{
Console.WriteLine(item);
}An extension could be that it allowed a third argument:
'[' expression '..' expression (',' expression)? ']'
This allows the developer to specify an increment step. The type of the argument must be implicitly convertible to T.
foreach(var radian in [0.0 .. Math.PI, Math.PI / 180])
{
DrawPoint(Math.Cos(radian) * radius, Math.Sin(radian) * radius);
}edit: removed left hand type inference.