10

I have an enum like this:

public enum ProductGroup
{
    A = 1,
    B = 2,
    C = 4,
    D = 8
}

How can I get ProductGroup enum values? For example, when my enum value is ProductGroup.C I want get its value 4;

4 Answers 4

15

Just cast it as an int to get the index value.

So:

ProductGroup productGroup = ProductGroup.C;

Int32 productGroupIndex = (Int32)productGroup;

In the above example, productGroupIndex == 4.

Sign up to request clarification or add additional context in comments.

2 Comments

just for the info there is no need of extra temporary variable here ........you can do it directly
"when my enum value is..." - from this, I made the assumption that it was an instance of the ProductGroup enum that would need the casting.
8

Just cast the enum

int val = (int)ProductGroup.A; 

Comments

5
 ProductGroup pg = ProductGroup.C;
 Console.Write((int)pg);

Comments

1

You can convert/cast the enum:

int productGroup = Convert.ToInt32(ProductGroup.C);

or

int productGroup = (int)ProductGroup.C;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.