|
-
September 30th, 2002, 12:55 PM
#1
Using enum values to index array without casting to (int)?
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....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|