2

I have an enum

public enum BookType
{
    Old = 'O',
    New = 'N',
    All = 'B'
}

What I need to do is get the value of the char in the enum. For example if the enum is set to:

BookType bt = BookType.New

I need to get the value of new "N"

string val = (???)bt;

I need val = N

What is the best way to do this? If it was an int easy, just cast to int.

Thanks.

2 Answers 2

5

The values associated with your enum are still ints, you've just set using a character literal. If you want to recover this value as a string, you can cast the enum value to a char and then convert that to a string:

string val = ((char)bt).ToString();
Sign up to request clarification or add additional context in comments.

Comments

3

You can just cast to char.
After casting to char, you'll need to call ToString() to convert the char to a string:

string val = ((char)bt).ToString();

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.