0

What am I not understanding here?

I can require that T is an enum, but i can't cast a variable of type T to int.

public static List<int> EnumToInts<T>() where T : System.Enum
{ ... }

And I can do this:

T te = (T)Enum.Parse(typeof(T), name);

But if I attempt to cast te to an int

int ti = (int)te

I get

'Cannot convert type T to int'

this is ok

var e = Enum.Parse(typeof(T), name);
int i = (int)e;

but the cast to int here is not:

T te = (T)Enum.Parse(typeof(T), name);
int ti = (int)te;

What's wrong here? What don't I understand?

        public static List<int> EnumToInts<T>() where T : System.Enum
        {
            List<int> l = new List<int>();

            Type type = typeof(T);

            string[] names = Enum.GetNames(type);

            foreach (var name in names)
            {

                var e = Enum.Parse(type, name);
                l.Add((int)e);

                T te = (T)Enum.Parse(type, name);
                l.Add((int)te);

            }

            return l;
        }
compound eye
  • 1,898
  • 18
  • 23
  • Why not simply use the Enumeration as the type? Why do you want to turn a perfectly good enum, into a ambigious int? – Christopher May 28 '20 at 14:03
  • IMHO this seems to be a more appropiate duplicate: https://stackoverflow.com/questions/908543 – Rand Random May 28 '20 at 14:04
  • @Servy - ping ping – Rand Random May 28 '20 at 14:07
  • Enumerations can use **any integral numeric type** (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types). It *defaults* to int, but is at no point limited it to. So you could be trying to cast anything from a sbyte to a ulong into int there. – Christopher May 28 '20 at 14:09
  • Treating a Enum as a int, ruins half the reason it is a enum to begin with. That you can not just set any value, is a major reason to use them(https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-1/). That they are basically compile time constants is the other big reason. Do not turn a perfectly good Enum into a ambigious integer. You can use a Enum as the key type in a Dictionary, if you need it for indexing/keys. – Christopher May 28 '20 at 14:10
  • 2
    "This is OK: `var e = Enum.Parse(typeof(T), name); int i = (int)e;`", actually it isn't if the enum doesn't use int as a backing type. – Lasse V. Karlsen May 28 '20 at 14:10
  • I'd use the followin: `Enum two = MyEnum.Two;int twoInt = Convert.ToInt32(two);` – Kevin Smith May 28 '20 at 14:14
  • @KevinSmith - this line will not work with `MyEnum` being the generic `T` it's not like you can write `T two = T.Two;` because of many reasons, eg. `T` (any enum) doesn't have `Two` – Rand Random May 28 '20 at 14:18
  • Hello, I'm not trying to get my code working, I'm trying to understand why i'm allowed to write '(int)e' when e could have any backing type, but even though I've specified 'where T : System.Enum' I can't cast T, when I'm assuming for the same reason I shouldn't be able to do '(int)e'? – compound eye May 28 '20 at 23:29
  • @compoundeye - you already got an answer as to why this doesn’t work, have another look into my proposed duplicate the first answer has the information you need - https://stackoverflow.com/questions/908543 - also Lasse‘s comment already pointed out that your cast won’t work in all cases no matter if generic or not, as to why it even compiles is simple because Enum.Parse returns an object and not System.Enum - https://learn.microsoft.com/en-us/dotnet/api/system.enum.parse – Rand Random May 29 '20 at 03:46
  • Thanks Rand Random, I understand why it would seem that I've got the answer, my question is how to make the code work, it's that I don't understand the principle behind allowing (int)obj and not (int)T when either could be invalid, it seems an inconsistent attempt to protect me from my own silliness, or is there a different aspect to this that I am not seeing? I feel there is something about the philosophy of .net I am skimming over by not understanding why the compiler does accept : int x = (int)(obj)(T)Enum.Parse(type, name); – compound eye May 30 '20 at 03:00

0 Answers0