in Technology by

How can an int be cast to an enum in C#?

1 Answer

0 votes
by

From an int:

YourEnum foo = (YourEnum)yourInt;

From a string:

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);

// The foo.ToString().Contains(",") check is necessary for enumerations marked with an [Flags] attribute
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
{
    throw new InvalidOperationException($"{yourString} is not an underlying value of the YourEnum enumeration.")
}

Update:

From number you can also

YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum) , yourInt);

Related questions

0 votes
    How can we enumerate an enum in C#?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    Im using EntityFramework Code First Generic Repository. I have one filter method. Also this method make paging ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    How to loop through all enum values in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    What does the Enum Attribute mean in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    Can S3 Be Cast-off With Ec2 Instances, In Case Of “yes” Please Specify How ?...
asked Mar 12, 2021 in Technology by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a 2 column (1 int and 1 double) dataframe "fit_comparison", of predicted values and linear ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have a number of user permissions that are tested throughout my ASP.NET application. These permission ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I need to cast a class to its protected base: class ComplicatedOne : public Object { //Lots of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    I use Eclipse for java, this is the error is got. Type safety: Unchecked cast from Object to HashMap Now there is ... how to use it? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have an Enum package com.javarnd.bbms.enums; public enum BloodTypeEnum { A_PLUS(1, "A+"), ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I am developing Outlook add-ins which populates all the available shared mailboxes in to combo box and sends ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I am having the following problem: I have an Enum that was originally declared with 5 elements. public enum ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I am developing Outlook add-ins which populates all the available shared mailboxes in to combo box and sends ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    How do I use the CAST in ADO to convert Float values to String? I tried SELECT CAST([Field] AS ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
...