Is there a .NET construct like Java's enum.ordinal()?

LSM07

If I have the following enum in F#:

type Note =
    | C = 0
    | D = 2
    | E = 4
    | F = 5
    | G = 7
    | A = 9
    | B = 11

But at some points, I need the ordinals of the items inside of it, such that Note.C = 1, Note.D = 2, etc. Obviously in other languages, foo.ordinal() would work, but in here is it necessary to use a function like such:

let getNoteOrdinal = function
    |Note.C -> 1
    |Note.D -> 2
    |Note.E -> 3
    |Note.F -> 4
    //etc

Is that function necessary or is there something better?

Charles Mager

Enums in .NET are just a layer on top of an integral type, in this case your values are int.

Per the docs, you can do a simple conversion to get that value, for example:

let value = int Note.B

Where value in this case would be 11.

To find the ordinal, as you can make use of a built-in function that gives you an array of all the values in order and then find the index of the one you're after. For example:

let getNoteOrdinal (note: Note) =
    let values = Enum.GetValues(typeof<Note>)
    Array.IndexOf(values, note) + 1

let value = getNoteOrdinal Note.E

Where value in this case would be 3.

You can take this one step further as @ReedCopsey suggests and make a generic function that will give you the ordinal for any enum value:

let getEnumOrdinal (e: 'a when 'a : enum<'b> and 'a : equality) =    
    let values = Enum.GetValues(e.GetType())
    Array.IndexOf(values, e) + 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Select enum by ordinal; java

Is it bad practice to use an Enum's ordinal value to index an array in Java?

Is there a Java built-in construct like vb.net's Do Until Loop?

Can Golang enum do same behavior like Java's enum?

Swift equivalent of Enum.ordinal() in Java

Can I specify ordinal for enum in Java?

Can you create a not-defined enum value in Java, like in .NET?

Can I have function types in Java's Enum like Swift?

Kotlin function to print all of an Enum's ordinal/name pairs

Using Enum's ordinal value in switch-case statement

Is there anything like .NET's NotImplementedException in Java?

Enum in Java like in C#

Can I give an enum an attribute in VB.NET (like I can do in Java)?

Enum class with name and ordinal constructors

Is it good practice to use ordinal of enum?

Ordinal of enum affecting BinaryFormatter deserialization?

How to cast an enum ordinal as a String?

JPA Enum ORDINAL vs STRING

Convert from enum ordinal to enum type

Implement something like an extendible enum in Java

How to make key value like enum in java

How to implement an enum with constructor in Swift like Java?

Java enum-like behavior in Rust

Is there a option in highcharts like the 'ordinal' in highstock?

How to construct string like new String('A', 30) in Java

Could not read JSON: Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist)

Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist)

Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist) using RabbitMQ

Is it possible to have the enum ordinal in kotlin without explicitly calling ordinal?