yesman
yesman

Reputation: 7829

C# - how do I find a key value inside an enum with a property?

Given the C# enum:

public enum options: byte
{
    yes = 0,
    no = 22,
    maybe = 62,
    foscho = 42
}

How do you retrieve the String 'maybe' if given the byte 62?

Upvotes: 2

Views: 174

Answers (2)

awj
awj

Reputation: 7949

var stringValue = Enum.GetName(typeof(options), 62); // returns "maybe"

Which you might also want to wrap in a check:

if (Enum.IsDefined(typeof(options), 62))
{
    var stringValue = Enum.GetName(typeof(options), 62);    // returns "maybe"`
}

MSDN link

Upvotes: 4

user2160375
user2160375

Reputation:

You can cast it to enum and retreive by ToString():

var result = ((options)62).ToString();

Upvotes: 8

Related Questions