Is it possible to use an index integer to obtain an enum value? For example, if…
public enum Days { Mon, Tues, Wed, ..., Sun};
…is it somehow possible to write something like…
Days currentDay = Days[index];
Thanks!
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.
No, but you can cast an
intto anenum, if the value you are using is defined in theenum, however you do this at your own risk:If you really want to be safe, you can check if it’s defined first, but that will involve some boxing, etc and will damper performance.
If you know your enum is a specified, contiguous value range (i.e. Mon = 0 thru Sun = 6) you can compare:
You can also use the array passed back by
Enum.GetValues(), but once again this is heavier than the cast: