tags:

views:

45

answers:

3

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);

This 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.

+6  A: 

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.

Mark Rushakoff
@Mark Thank you. I just figured it out.
Curtis White
A: 

Oops I see Enumerable takes a count and not a start to end. This appears to work if start is from 1.

Curtis White
+3  A: 

Instead, call Enum.GetValues, like this:

IEnumerable<int> _myTypes = (int[])Enum.GetValues(typeof(MyEnum));
SLaks