From this sample on MSDN:
[Flags]
public enum Pet {
None = 0,
Dog = 1,
Cat = 2,
Bird = 4,
Rabbit = 8,
Other = 16
}
Is there anyway to let the enum generates its values automatically ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming that you really want a “flags” enum, there’s no automated way of doing this, no. The
Petexample doesn’t seem like a good fit for flags, but I’m assuming that’s just an unfortunate choice of example. One option which reduces the possibility for error slightly is to use bit operations:If everything uses bit shifting, you know you won’t accidentally end up with a value which is actually multiple bits.