tags:

views:

1180

answers:

2
+8  A: 

You need to cast the result to the actual array type you want

(Response[])Enum.GetValues(typeof(Response))

as GetValues isn't strongly typed

EDIT: just re-read the answer. You need to explicitly cast each enum value to the underlying type, as GetValues returns an array of the actual enum type rather than the base type. Enum.GetUnderlyingType could help with this.

thecoop
Also, just to reiterate the point, 'var' is strongly-typed. It won't magically get set to the actual type of the result.
Roger Lipscombe
Regarding your edit : no, it's not necessary to cast each value individually. Casting the array as you wrote in your code works fine
Thomas Levesque
+5  A: 

Can you please refer to the documentation you mention. The MSDN documentation on Enum.GetValues does not mention anything like that (quote from that page):

Return Value

Type: System.Array

An Array of the values of the constants in enumType. The elements of the array are sorted by the binary values of the enumeration constants.

Fredrik Mörk
Sorry I used the word documentation. I should have said it was an online article I read at this url:http://www.csharp-station.com/Tutorials/Lesson17.aspx
Cragly
@Cragly: The result returned from `Enum.GetValues` in that sample will be of the type `Volume[]`. However, note how the for loop is set up: `foreach (byte val in Enum.GetValues(...`. This means that the code will cast each `Volume` value in the array to a `byte`.
Fredrik Mörk