I see the Enum.GetValues
returns base Array
type and Enum.GetNames
returns a string
array. But I don't understand how this is very significant. For an enum
anyways, the values are strings.
But, there is an associated problem. The DataGridView
ComboBox
column databinds to an enum
if I give the DataSource
= Enum.GetValues
, but it doesn't databind when I give it Enum.GetNames
as a DataSource
.
The GetValues method returns an array that contains a value for each member of the enumType enumeration. If multiple members have the same value, the returned array includes duplicate values.
Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.
Enums in C# The enum keyword in C# declares a list of named integer constants. An enum can be defined in a namespace, structure or class. However, it is better to define it in a namespace so that all the classes can access it.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With