1

in JavaScript world you can get an enum value according to its index in the object (not the value assigned to the enum member, but always the nth member of that enum):

const myEnum = {
    Hello: 1,
    Bye: 2,
    Greeting: 3
}
const value = myEnum[Object.keys(myEnum)[0]];
console.log(value) // it returns 1

I was wondering if it's possible to have this kind of behavior in C# too.

I am trying to find the nth member of an Enum in C# and the values in it are all different and there is no order to them (and that's exactly how I want them to be).

Update

enum CSharpEnum {
    SomeValue = 4,
    AnotherValue = 2,
    AndAnotherOne = 1
}

I get some indexes (like n) from somewhere else and I want to get the nth memeber of CSharpEnum. An example:

var index = 2;
var member // a way to get the member and it should return 1 (AndAnotherOne)
// because it is the third member (0, 1, 2)

Update 2

It seems my question is not clear enough so here is a link to a dotnetfiddle playground. In one of the answers there was a GetValues method which made a list of enum values but the enum members got rearranged in it. I have an enum in the playground and I want to get the third (0, 1, 2) member for example which is Want. Is there a way I can get that?

  • Your example doesn't look like it's showing your intention? What is it you actually want here? Order of appearance in source code or order of values? In order to make your intention clear, you could provide an example of desired result, where the source order is mixed (like `enum { XSecond = 3, Third = 9, First = 1}` I placed the `X` in front of second, so it can't be mistaken for alphabetical order of names) – grek40 May 16 '21 at 15:58
  • I updated the post. @grek40 I hope it's clear now what I seek. – Mehran Mirshekaran May 16 '21 at 17:44
  • Thanks for clarification. (as far as I can tell) there is no reliable way without modifying the enum. Typical solutions on the net rely on implementation details that are not guaranteed by the specification or use explicit annotations. If you want a specific order that is different from the ordering of values or names, you should explicitly annotate the enum members with the desired order. – grek40 May 16 '21 at 18:04
  • So it seems I should think of a workaround. Thank you. – Mehran Mirshekaran May 17 '21 at 05:23
  • 1
    https://stackoverflow.com/a/46608102/34092 – mjwills May 18 '21 at 13:00
  • 2
    There is no *reliable* way to do that. There are various ways based on getting the enum's fields that *happen to work* for current versions of the compiler, but relying on it would be foolish. You can get this to work reliably at compile time if you parse the code yourself with Roslyn rather than inspecting the type, but that's more in the realm of code generators. A C# enum is not (explicitly) an ordered collection of tags like it is in JavaScript. – Jeroen Mostert May 18 '21 at 13:10

2 Answers2

0

Given

public enum myEnum
{
    Hello = 1,
    Bye = 2,
    Greeting = 3
}

the Enum Class has a static method GetValues(Type) that returns an array TEnum[] of the values of this enum:

myEnum[] enumArray = (myEnum[])Enum.GetValues(typeof(myEnum));
myEnum value = enumArray[i]; // { [0] = Hello, [1] = Bye, [3] = Greeting }

Newer versions of the Framework have a generic overload (since .NET Framework 5.0?):

myEnum[] enumArray = Enum.GetValues<myEnum>();
myEnum value = enumArray[i];

UPDATE

The purpose of an enumeration type is to provide a set of named constants having an underlying integral numeric type. These constants are not indexed and have no particular order defined. If you want to have them indexed, insert them into an array

enum myEnum {
    SomeValue = 4,
    AnotherValue = 2,
    AndAnotherOne = 1
}
static readonly myEnum[] enumArray = new[] {
    myEnum.SomeValue,
    myEnum.AnotherValue,
    myEnum.AndAnotherOne
};
myEnum value = enumArray[2]; // --> myEnum.AndAnotherOne

See also: Enumeration types (C# reference)

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Will [EnumsNet](https://github.com/TylerBrinkley/Enums.NET) become obsolete with .NET 6? –  May 16 '21 at 15:10
  • 1
    I never used it, but according to the Read.me file of [Enums.NET](https://github.com/TylerBrinkley/Enums.NET), it provides much more functionality than just this generic `GetValues`. – Olivier Jacot-Descombes May 16 '21 at 15:21
  • The problem is the values get rearranged into the proper order `enumArray = [AndAnotherOne, AnotherValue, SomeValue]` – Mehran Mirshekaran May 17 '21 at 05:12
  • They get rearranged according to their numeric value. But in your example, they keep the original order `{ [0] = Hello, [1] = Bye, [3] = Greeting }`, where `[n]` are the array indexes. If you want the numeric value of an enum instead, you can cast it to `int` with `int i = (int)enumValue;`. – Olivier Jacot-Descombes May 17 '21 at 12:17
  • Would you be so kind to look at the playground I've added to the question? I've updated the question. Maybe now it is more clear what I seek?! – Mehran Mirshekaran May 18 '21 at 12:40
0

With enum

public enum Test
{
    hello,
    world
}

Create an array with

var enums = (Test[])Enum.GetValues(typeof(Test));

And now it's indexable.

Jannick Breunis
  • 195
  • 2
  • 14