Click to See Complete Forum and Search --> : Using enum values to index array without casting to (int)?


sechols
September 30th, 2002, 12:55 PM
Hello!

I would like to be able to access an simple array using an enum value without having to cast to (int). Is this possible?

Here's a silly example:

public enum Fruit : int { Apple = 0, Orange, Banana };

private string[] fruitcolor = { "Red", "Orange of course", "Yellow" };

Console.WriteLine( fruitcolor[Fruit.Banana] ); // Cannot implicitly convert enum to int error.

Console.WriteLine( fruitcolor[(int)Fruit.Banana] ); // Works, but I don't want to have to cast.

I thought that by specifying the type for the enum as int, it would fix the problem, but nothing seems to work except explicitly casting.

Any pointers would be greatly appreciated....