I see the Enum.GetValues
returns base Array
type and Enum.GetNames
returns a string
array. But I don\'t understand how th
GetValues will return an Array of the underlying integer values for each item in the Enum.
GetNames will return a string array of the Names for the items in the enum.
The Array returned by GetValues implements IList while the string[] returned by GetNames does not, which explains the binding differences.
Enums are actually numeric. GetNames
returns the field names. GetValues
returns the numeric values.
MSDN has a great sample on GetValues.
Think of enumerations as Name/Value pairs.
enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };
In the example above, GetNames()
will return a string
array containing the items "Negative", "Zero", and "Positive." GetValues()
will return an array of SignMagnitude
containing SignMagnitude.Negative
, SignMagnitude.Zero
and SignMagnitude.One
.
There is an example of binding Enum names to a dropdown in a DataGridView here: Create drop down list options from enum in a DataGridView