I have a class called Questions
(plural). In this class there is an enum called Question
(singular) which looks like this.
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
In the Questions
class I have a get(int foo)
function that returns a Questions
object for that foo
. Is there an easy way to get the integer value off the enum so I can do something like this Questions.Get(Question.Role)
?
get(int foo)
you can define it as get(Question foo)
then do your casting inside the method, the you can call your method as Questions.Get(Question.Role)
- anyone Just cast the enum, e.g.
int something = (int) Question.Role;
The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int
.
However, as cecilphillip points out, enums can have different underlying types.
If an enum is declared as a uint
, long
, or ulong
, it should be cast to the type of the enum; e.g. for
enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649};
you should use
long something = (long)StarsInMilkyWay.Wolf424B;
Answered 2023-09-20 20:33:42
enum Test { Item = 1 }
and see that 1 == (int)Test.Item
is equal. - anyone (int)Test.Item
That is a cast! () is the explicit cast operator. - anyone enum Question
was not int
but long
this cast will truncate Role
s integral value! - anyone Enum
as a parameter, you know is only a fixed number of possible integral values you can get. On the other hand, if you take simply an int
, then you have to validate if that int
is within the accepted values., thus complicating the code. You can always override your signatures like ``` public void MyMethod(int x) { // do something with x } public void MyMethod(Enum x) { this.MyMethod((int) x); } ```` - anyone Since Enums can be any integral type (byte
, int
, short
, etc.), a more robust way to get the underlying integral value of the enum would be to make use of the GetTypeCode
method in conjunction with the Convert
class:
enum Sides {
Left, Right, Top, Bottom
}
Sides side = Sides.Bottom;
object val = Convert.ChangeType(side, side.GetTypeCode());
Console.WriteLine(val);
This should work regardless of the underlying integral type.
Answered 2023-09-20 20:33:42
var
is of type object
, so you need to unbox it and it gets messier than I would like. - anyone Declare it as a static class having public constants:
public static class Question
{
public const int Role = 2;
public const int ProjectFunding = 3;
public const int TotalEmployee = 4;
public const int NumberOfServers = 5;
public const int TopBusinessConcern = 6;
}
And then you can reference it as Question.Role
, and it always evaluates to an int
or whatever you define it as.
Answered 2023-09-20 20:33:42
static readonly int
because constants are compiled into their hard values. See stackoverflow.com/a/755693/492 - anyone const
and not static readonly
because every time you compare static readonly
you're making a method call to get the value of the variable whereas with a const
you're comparing two value types directly. - anyone compilation limitation
I mean that the value is hard-coded when you compile it so any change to that value would require that all assemblies using it need to be recompiled. I'm inclined to agree with you about usage because changing the values would have far-reaching implications. - anyone On a related note, if you want to get the int
value from System.Enum
, then given e
here:
Enum e = Question.Role;
You can use:
int i = Convert.ToInt32(e);
int i = (int)(object)e;
int i = (int)Enum.Parse(e.GetType(), e.ToString());
int i = (int)Enum.ToObject(e.GetType(), e);
The last two are plain ugly. I prefer the first one.
Answered 2023-09-20 20:33:42
Cannot convert type 'System.Enum' to 'int'
- anyone Question question = Question.Role;
int value = (int) question;
Will result in value == 2
.
Answered 2023-09-20 20:33:42
Example:
public enum EmpNo
{
Raj = 1,
Rahul,
Priyanka
}
And in the code behind to get the enum value:
int setempNo = (int)EmpNo.Raj; // This will give setempNo = 1
or
int setempNo = (int)EmpNo.Rahul; // This will give setempNo = 2
Enums will increment by 1, and you can set the start value. If you don't set the start value it will be assigned as 0 initially.
Answered 2023-09-20 20:33:42
1
in int
except you explicitly define otherwise - anyone It's easier than you think - an enum is already an int. It just needs to be reminded:
int y = (int)Question.Role;
Console.WriteLine(y); // Prints 2
Answered 2023-09-20 20:33:42
I have recently converted away from using enums in my code in favour of instead using classes with protected constructors and predefined static instances (thanks to Roelof - C# Ensure Valid Enum Values - Futureproof Method).
In light of that, below's how I'd now approach this issue (including implicit conversion to/from int
).
public class Question
{
// Attributes
protected int index;
protected string name;
// Go with a dictionary to enforce unique index
//protected static readonly ICollection<Question> values = new Collection<Question>();
protected static readonly IDictionary<int,Question> values = new Dictionary<int,Question>();
// Define the "enum" values
public static readonly Question Role = new Question(2,"Role");
public static readonly Question ProjectFunding = new Question(3, "Project Funding");
public static readonly Question TotalEmployee = new Question(4, "Total Employee");
public static readonly Question NumberOfServers = new Question(5, "Number of Servers");
public static readonly Question TopBusinessConcern = new Question(6, "Top Business Concern");
// Constructors
protected Question(int index, string name)
{
this.index = index;
this.name = name;
values.Add(index, this);
}
// Easy int conversion
public static implicit operator int(Question question) =>
question.index; //nb: if question is null this will return a null pointer exception
public static implicit operator Question(int index) =>
values.TryGetValue(index, out var question) ? question : null;
// Easy string conversion (also update ToString for the same effect)
public override string ToString() =>
this.name;
public static implicit operator string(Question question) =>
question?.ToString();
public static implicit operator Question(string name) =>
name == null ? null : values.Values.FirstOrDefault(item => name.Equals(item.name, StringComparison.CurrentCultureIgnoreCase));
// If you specifically want a Get(int x) function (though not required given the implicit converstion)
public Question Get(int foo) =>
foo; //(implicit conversion will take care of the conversion for you)
}
The advantage of this approach is you get everything you would have from the enum, but your code's now much more flexible, so should you need to perform different actions based on the value of Question
, you can put logic into Question
itself (i.e. in the preferred OO fashion) as opposed to putting lots of case statements throughout your code to tackle each scenario.
NB: Answer updated 2018-04-27 to make use of C# 6 features; i.e. declaration expressions and lambda expression body definitions. See revision history for original code. This has the benefit of making the definition a little less verbose; which had been one of the main complaints about this answer's approach.
Answered 2023-09-20 20:33:42
If you want to get an integer for the enum value that is stored in a variable, for which the type would be Question
, to use for example in a method, you can simply do this I wrote in this example:
enum Talen
{
Engels = 1, Italiaans = 2, Portugees = 3, Nederlands = 4, Duits = 5, Dens = 6
}
Talen Geselecteerd;
public void Form1()
{
InitializeComponent()
Geselecteerd = Talen.Nederlands;
}
// You can use the Enum type as a parameter, so any enumeration from any enumerator can be used as parameter
void VeranderenTitel(Enum e)
{
this.Text = Convert.ToInt32(e).ToString();
}
This will change the window title to 4, because the variable Geselecteerd
is Talen.Nederlands
. If I change it to Talen.Portugees
and call the method again, the text will change to 3.
Answered 2023-09-20 20:33:42
Use an extension method instead:
public static class ExtensionMethods
{
public static int IntValue(this Enum argEnum)
{
return Convert.ToInt32(argEnum);
}
}
And the usage is slightly prettier:
var intValue = Question.Role.IntValue();
Answered 2023-09-20 20:33:42
One more way to do it:
Console.WriteLine("Name: {0}, Value: {0:D}", Question.Role);
It will result in:
Name: Role, Value: 2
Answered 2023-09-20 20:33:42
To ensure an enum value exists and then parse it, you can also do the following.
// Fake Day of Week
string strDOWFake = "SuperDay";
// Real Day of Week
string strDOWReal = "Friday";
// Will hold which ever is the real DOW.
DayOfWeek enmDOW;
// See if fake DOW is defined in the DayOfWeek enumeration.
if (Enum.IsDefined(typeof(DayOfWeek), strDOWFake))
{
// This will never be reached since "SuperDay"
// doesn't exist in the DayOfWeek enumeration.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWFake);
}
// See if real DOW is defined in the DayOfWeek enumeration.
else if (Enum.IsDefined(typeof(DayOfWeek), strDOWReal))
{
// This will parse the string into it's corresponding DOW enum object.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWReal);
}
// Can now use the DOW enum object.
Console.Write("Today is " + enmDOW.ToString() + ".");
Answered 2023-09-20 20:33:42
public enum QuestionType
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
...is a fine declaration.
You do have to cast the result to int like so:
int Question = (int)QuestionType.Role
Otherwise, the type is still QuestionType
.
This level of strictness is the C# way.
One alternative is to use a class declaration instead:
public class QuestionType
{
public static int Role = 2,
public static int ProjectFunding = 3,
public static int TotalEmployee = 4,
public static int NumberOfServers = 5,
public static int TopBusinessConcern = 6
}
It's less elegant to declare, but you don't need to cast it in code:
int Question = QuestionType.Role
Alternatively, you may feel more comfortable with Visual Basic, which caters for this type of expectation in many areas.
Answered 2023-09-20 20:33:42
Use:
Question question = Question.Role;
int value = question.GetHashCode();
It will result in value == 2
.
This is only true if the enum fits inside an int
.
Answered 2023-09-20 20:33:42
int
of course, as GetHashCode
returns an integer. - anyone Maybe I missed it, but has anyone tried a simple generic extension method?
This works great for me. You can avoid the type cast in your API this way but ultimately it results in a change type operation. This is a good case for programming Roslyn to have the compiler make a GetValue<T> method for you.
public static void Main()
{
int test = MyCSharpWrapperMethod(TestEnum.Test1);
Debug.Assert(test == 1);
}
public static int MyCSharpWrapperMethod(TestEnum customFlag)
{
return MyCPlusPlusMethod(customFlag.GetValue<int>());
}
public static int MyCPlusPlusMethod(int customFlag)
{
// Pretend you made a PInvoke or COM+ call to C++ method that require an integer
return customFlag;
}
public enum TestEnum
{
Test1 = 1,
Test2 = 2,
Test3 = 3
}
}
public static class EnumExtensions
{
public static T GetValue<T>(this Enum enumeration)
{
T result = default(T);
try
{
result = (T)Convert.ChangeType(enumeration, typeof(T));
}
catch (Exception ex)
{
Debug.Assert(false);
Debug.WriteLine(ex);
}
return result;
}
}
Answered 2023-09-20 20:33:42
int number = Question.Role.GetHashCode();
number
should have the value 2
.
Answered 2023-09-20 20:33:42
bool
*, byte
, ushort
, int
, and uint
**. The GetHashCode for other types modifies the value, e.g. does some bitshifts and xors. (* 1 / 0, **casted to int of course). But all in all just don't do this unless it's your own private code. - anyone You can do this by implementing an extension method to your defined enum type:
public static class MyExtensions
{
public static int getNumberValue(this Question questionThis)
{
return (int)questionThis;
}
}
This simplifies getting the int value of the current enum value:
Question question = Question.Role;
int value = question.getNumberValue();
or
int value = Question.Role.getNumberValue();
Answered 2023-09-20 20:33:42
public enum Suit : int
{
Spades = 0,
Hearts = 1,
Clubs = 2,
Diamonds = 3
}
Console.WriteLine((int)(Suit)Enum.Parse(typeof(Suit), "Clubs"));
// From int
Console.WriteLine((Suit)1);
// From a number you can also
Console.WriteLine((Suit)Enum.ToObject(typeof(Suit), 1));
if (typeof(Suit).IsEnumDefined("Spades"))
{
var res = (int)(Suit)Enum.Parse(typeof(Suit), "Spades");
Console.Out.WriteLine("{0}", res);
}
Answered 2023-09-20 20:33:42
Since enums can be declared with multiple primitive types, a generic extension method to cast any enum type can be useful.
enum Box
{
HEIGHT,
WIDTH,
DEPTH
}
public static void UseEnum()
{
int height = Box.HEIGHT.GetEnumValue<int>();
int width = Box.WIDTH.GetEnumValue<int>();
int depth = Box.DEPTH.GetEnumValue<int>();
}
public static T GetEnumValue<T>(this object e) => (T)e;
Answered 2023-09-20 20:33:42
You should have used Type Casting as we can use in any other language.
If your enum
is like this-
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
And you need to cast to an int
, then do this-
Question q = Question.Role;
.............
.............
int something = (int) q;
In C#, there are two types of casting:
char
->int
->long
->float
->double
double
->float
->long
->int
->char
More can be found in here.
Answered 2023-09-20 20:33:42
The easiest solution I can think of is overloading the Get(int)
method like this:
[modifiers] Questions Get(Question q)
{
return Get((int)q);
}
where [modifiers]
can generally be same as for the Get(int)
method. If you can't edit the Questions
class or for some reason don't want to, you can overload the method by writing an extension:
public static class Extensions
{
public static Questions Get(this Questions qs, Question q)
{
return qs.Get((int)q);
}
}
Answered 2023-09-20 20:33:42
My favourite hack with int or smaller enums:
GetHashCode();
For an enum
public enum Test
{
Min = Int32.MinValue,
One = 1,
Max = Int32.MaxValue,
}
This,
var values = Enum.GetValues(typeof(Test));
foreach (var val in values)
{
Console.WriteLine(val.GetHashCode());
Console.WriteLine(((int)val));
Console.WriteLine(val);
}
outputs
one
1
1
max
2147483647
2147483647
min
-2147483648
-2147483648
Disclaimer:
It doesn't work for enums based on long.
Answered 2023-09-20 20:33:42
The example I would like to suggest "to get an 'int' value from an enum", is
public enum Sample
{
Book = 1,
Pen = 2,
Pencil = 3
}
int answer = (int)Sample.Book;
Now the answer will be 1.
Answered 2023-09-20 20:33:42
Try this one instead of convert enum to int:
public static class ReturnType
{
public static readonly int Success = 1;
public static readonly int Duplicate = 2;
public static readonly int Error = -1;
}
Answered 2023-09-20 20:33:42
Following is the extension method
public static string ToEnumString<TEnum>(this int enumValue)
{
var enumString = enumValue.ToString();
if (Enum.IsDefined(typeof(TEnum), enumValue))
{
enumString = ((TEnum) Enum.ToObject(typeof (TEnum), enumValue)).ToString();
}
return enumString;
}
Answered 2023-09-20 20:33:42
In Visual Basic, it should be:
Public Enum Question
Role = 2
ProjectFunding = 3
TotalEmployee = 4
NumberOfServers = 5
TopBusinessConcern = 6
End Enum
Private value As Integer = CInt(Question.Role)
Answered 2023-09-20 20:33:42
will give you the a list with all the integer values of the enum :
List enumValues = Enum.GetValues(typeof(EnumClass)).Cast().ToList();
Answered 2023-09-20 20:33:42
public enum ViewType
{
List = 1,
Table = 2,
};
// You can use the Enum type as a parameter, so any enumeration from any enumerator
// cshtml
// using proyects.Helpers
// @if (Model.ViewType== (int)<variable>.List )
Answered 2023-09-20 20:33:42
Nearly all of the current answers use Convert
or cast to object before the target type, which results in boxing and unboxing operations. This causes heap allocations and may not be acceptable for hot paths.
Since the majority of these answers were written, System.Runtime.CompilerServices.Unsafe was introduced, enabling low-level manipulation of pointers.
In combination with generics, the Unsafe
class allows us to change the underlying type of the System.Enum
parameter safely, with zero allocations, and with performance that is nearly indistinguishable from an empty method duration:
public long GetEnumValue<T>(T enumInstance) where T : unmanaged, Enum
{
var size = Unsafe.SizeOf<T>();
if (size == Unsafe.SizeOf<byte>())
{
return Unsafe.As<T, byte>(ref enumInstance);
}
else if (size == Unsafe.SizeOf<short>())
{
return Unsafe.As<T, short>(ref enumInstance);
}
else if (size == Unsafe.SizeOf<int>())
{
return Unsafe.As<T, int>(ref enumInstance);
}
else if (size == Unsafe.SizeOf<long>())
{
return Unsafe.As<T, long>(ref enumInstance);
}
return -1; // or throw if you prefer
}
If it is preferable to always return an int, you can do so, although if the backing field of the enum exceeds int.MaxValue
, it will overflow:
public int GetEnumValue<T>(T enumInstance) where T : unmanaged, Enum
{
var size = Unsafe.SizeOf<T>();
if (size == Unsafe.SizeOf<byte>())
{
return Unsafe.As<T, byte>(ref enumInstance);
}
else if (size == Unsafe.SizeOf<short>())
{
return Unsafe.As<T, short>(ref enumInstance);
}
else if (size == Unsafe.SizeOf<int>())
{
return Unsafe.As<T, int>(ref enumInstance);
}
else if (size == Unsafe.SizeOf<long>())
{
return Unsafe.As<T, int>(ref enumInstance);
}
return -1; // or throw if you prefer
}
If the following were used:
public enum EnumLong : long
{
First,
Second,
Third = (long)(int.MaxValue) + 1
}
var val = GetEnumValue(EnumLong.Third);
-2147483648
would be returned due to an overflow. It is extremely unlikely that developers would create enums with such large flag values, but is always a possibility.
Answered 2023-09-20 20:33:42