Convert Enum variable to int in C#

Asked

Viewed 841 times

2

I have the following code:

enum type = { OPEN = 0, CLOSED, UNDEFINED};
list<int> frequencia = new list<int>(new int[] {0,0,0});

I would later like to carry out the following operation:

type t = enum.OPEN;
frequencia[t]++;

but I can’t. Any solution on how to convert the Enum variable to int?

  • 6

    Just do a type-cast: (int)enumValue.

  • 5

    In C# the default encoding is to use "Open, Closed, Undefined" without ALL CAPS.

  • @Miguelangelo, that’s just what I needed. Thank you!

  • @bigdown, thanks for the tip. I’ll adopt it.

1 answer

6


Do it this way:

int t = (int)enum.OPEN;
frequencia[t]++;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.