Difference between Enum.GetValues and Enum.GetNames

后端 未结 3 818
故里飘歌
故里飘歌 2020-12-16 13:48

I see the Enum.GetValues returns base Array type and Enum.GetNames returns a string array. But I don\'t understand how th

相关标签:
3条回答
  • 2020-12-16 14:15

    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.

    0 讨论(0)
  • 2020-12-16 14:35

    Enums are actually numeric. GetNames returns the field names. GetValues returns the numeric values.

    MSDN has a great sample on GetValues.

    0 讨论(0)
  • 2020-12-16 14:40

    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

    0 讨论(0)
提交回复
热议问题