0

Possible Duplicate:
Why enums require an explicit cast to int type?

I defined this enumeration:

public enum GroupTypes : int
{
    gt_table0 = 0
    gt_table1 = gt_table0 + 1,
    gt_table2 = gt_table1 + 1,
    gt_table3 = gt_table2 + 1,
    gt_table4 = gt_table3 + 1,
    gt_table5 = gt_table4 + 1,
    gt_table6 = gt_table5 + 1,
    gt_table7 = gt_table6 + 1,
    gt_table8 = gt_table7 + 1
}

I then use this enum as follows:

for (int i = 0; i < NbrTables; i++)
{
    // Compute starting table
    int StartGroup = GroupTypes.gt_table0 + i;

    // Some more code here        
}

Where I am confused is when I use the enumeration to compute my StartGroup, I get the error "Cannot implicitly convert type 'GroupTypes' to 'int'. An explicit conversion exists'.

I thought that I would avoid this error since I declared the GroupTypes enum as an int.

Where is my mis-understanding?

Community
  • 1
  • 1
Jim Lahman
  • 2,691
  • 2
  • 25
  • 21

2 Answers2

6

I thought that I would avoid this error since I declared the GroupTypes enum as an int.

Since your enum is an int, you can do this if you specify the cast explicitly:

int StartGroup = (int)GroupTypes.gt_table0 + i;
heisenberg
  • 9,665
  • 1
  • 30
  • 38
2

The use of : int only affects the storage class of the enum, but not any of its behaviour

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30