How do I cast a generic enum to int?
try this,
public void SetOptions<T>()
{
Type genericType = typeof(T);
if (genericType.IsEnum)
{
foreach (T obj in Enum.GetValues(genericType))
{
Enum test = Enum.Parse(typeof(T), obj.ToString()) as Enum;
int x = Convert.ToInt32(test); // x is the integer value of enum
..........
..........
}
}
}
Can't cast an `Enum` value to `int` in a generic method
enum can be a long or other things
public static void Stuff<T>(T val) where T : System.Enum, IConvertible
{
int v = val.ToInt32(null);
}
This works
If you look at Enum you can see that its not stated as an int.
The actual base classes and implementations are:
public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible
BTW: prefer long over int since longs are used as Flags in enumns to allow 64 flags
Cast Int to Generic Enum in C#
The simplest way I have found is to force the compiler's hand by adding a cast to object
.
return (T)(object)i.Value;
How to cast my own enum type to generic enum type?
The simplest way to cast your custom enum type MyEnum
to generic enum type TEnum
is to use the next approach:
case MyEnum _: return (TEnum) (object) MyEnum.First;
Here are links to similar problems:
- Value of type 'T' cannot be converted to
- How do I cast a generic enum to int?
- Cast Int to Generic Enum in C#
Cast enum-value in generic method to int
I think, it's better to use enum-specific format string to get integer representation of enum (integer in general meaning, not only int32)
fieldTextList.Add($"F{field:D}");
You can find more about format specifiers for enums here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/enumeration-format-strings#d-or-d
How to cast a variable of an enum generic type to int and back
You may add IConvertible
to generic constraint and use ToInt32
method, something like
class EnumWrapper<T>
where T : Enum, IConvertible
{
void DoSomething(T arg)
{
int a = arg.ToInt32(null);
}
}
Have a look at the Enum
constraint for details. Also, as mentioned in comments, it isn't necessary that your enum
is based on int
type
cast integer to generic enum types that have a different underlying type
You can do that for example like this:
public static T Cast<T>(int i) where T : Enum {
return (T)Convert.ChangeType(i, Enum.GetUnderlyingType(typeof(T)));
}
Can I cast from a generic type to an enum in C#?
Like this:
return (T)(object)value;
C# generic enum cast to specific enum
There is no valid cast from an arbitrary type to an enum type so this is not allowed. You need to cast to object first:
FirstEnumType t = (FirstEnumType)(object)type;
This "tricks" the compiler by upcasting to object
(which is always valid) then down-casts to the enum type. Assuming you have done a runtime type check, the downcast will never fail. However implementing this in the else branch, as given, isn't guaranteed to work.
One would question why the method is even generic in the first place but that is how you can make this particular method work.
Related Topics
Can't See Localhost from Uwp App
How to Get Modified Date from File in C# on Windows Mobile
How to Sort a List<T> by Multiple T.Attributes
Linq: from a List of Type T, Retrieve Only Objects of a Certain Subclass S
Accessing UI Controls in Task.Run with Async/Await on Winforms
Get Access to Parent Control from User Control - C#
Is There an Entity Framework 7 Database-First Poco Generator
Open File in Exclusive Mode in C#
How to Convert a Simple .Net Console Project a into Portable Exe with Mono and Mkbundle
Datagrid Column Width Doesn't Auto-Update
Wpf Datagrid Get Selected Cell Value
Are .Net Switch Statements Hashed or Indexed
Preventing Jit Inlining on a Method
In C# Why Can't a Conditional Operator Implicitly Cast to a Nullable Type