0

I have this enum:

public enum Lang
{
    English = 0,
    Romaji = 1,
    Kana = 2,
    Kanji = 3,
}

I know I can write:

var a = Lang.English
var b = a; 

and set b to be equal to 0. However is there any way that I can set b equal to "English" if given a?

4
  • Please clarify. To my understanding, the above statements would already set b to Lang.English, which would correspond to an integer value of 0. Commented Nov 26, 2016 at 12:05
  • 1
    Do you mean b.ToString() ? Commented Nov 26, 2016 at 12:06
  • var b = a.ToString() Commented Nov 26, 2016 at 12:06
  • Basically, assume printing a would print "English", he wants b to do the same instead of printing 0. Commented Nov 26, 2016 at 12:06

1 Answer 1

5

Essentially an enum is a set of named constants. So instead of using the constants, you use their names to make your code more readable.

The value of b would be equal to the named constant Lang.English and it's type wouldn't be of a string but it would be of Lang. So casting b to an int, you would get 0.

However is there any way that I can set b equal to "English" if given a?

Yes, you could try the following:

string b = Enum.GetName(typeof(Lang),Lang.English);

For a demo, please have a look here

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

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.