Bug in enumerable.range?
I have this code:
public enum MyEnum
{
First = 6,
Data1 = 6,
Data2 = 7,
Data3 = 8,
Data4 = 9,
Data5 = 10,
Last = 10,
Invalid = -1
};
Enumerable<int> _myTypes = Enumerable.Range((int)MyEnum.First, (int)MyEnum.Last);
Thi开发者_Python百科s creates an enumerable with elements from 6 to 15. I have equivalent code starting with 1 and it works as expected. This seems like a bug or very strange to me.
Enumerable.Range
takes a start
value, and a count
value, not a start
and end
value.
So you are telling it to start at 6 and take 10 units, hence 6-15.
Instead, call Enum.GetValues
, like this:
IEnumerable<int> _myTypes = (int[])Enum.GetValues(typeof(MyEnum));
Oops I see Enumerable takes a count and not a start to end. This appears to work if start is from 1.
精彩评论