   [.net] C# Enum Indexed Array
  It suddenly occurred to me that I was doing a lot of the following in my project:enum AbilityType {Strength, Agility, Intelligence};int[] Scores = new int[3];Scores[(int)AbilityType.Strength] = 15;...That is, indexing arrays by enum constants, either directly as above, or through the use of indexer in a containing class.  I saw 3 problems with this:1:  Extending the enums, for example adding "Endurance" above, I would have to update the magic numbers for the array sizes manually2:  Enums such as WallType {none=0, brick = 1, arch = 2, door = 4} obviously presents some problems3:  doing Scores[4] is too easy to do, not even intellisense helps me here.4:  Using indexers partially solves 3, but now I am writing essentially the same code every time (except with different types)So, I came up with something like this:    public class EnumIndexedArray<T,E>    {   static Type enumType;   Dictionary<E, T> table = new Dictionary<E, T>();   // enforce E to be an enum type during first chance at runtime, since   // as far as I can tell there are no 'enum' constraints   static EnumIndexedArray()   {   System.Diagnostics.Debug.Assert(typeof(E).IsEnum,"EnumIndexedArray<E>, E must be an enum type");   enumType = typeof(E);   }   // build dictionary, set to default values of T   public EnumIndexedArray()   {  Array values = System.Enum.GetValues(enumType);    foreach (object val in values)  { E key = (E)val; if(!table.ContainsKey(key))table.Add(key, default(T));  }   }   // provide array-like access   public T this[E index]   {  get  { return (T)table[index];  }  set  { table[index] = value;  }   }}Now I can do this:EnumIndexedArray<int, AbilityType> baseScores= new EnumIndexedArray<int,AbilityType>()baseScores[AbilityType.Strength] = 15;and seem to have eliminated problems 1-4 above.However, before buttering up this class any more (and since I am new to C#/.NET, and generics in particular), I thought I'd post here to see what you more knowledgeable fellows thought about this.  In particular, I'm wondering:A:  Is there a better/more efficient/more idiomatic way of implementing this?B:  Are there gross efficiency flaws here? (I know I'm sacrificing efficiency for abstraction here, and this will obviously not be used inside sensitive loops)C:  Are there suggested obvious functionality I should add?  Exception handling, iterator, possibly a ToArray() all come to mind...D:  Later on, will making this [Serializable] cause problems?  I assume that if the type T is not also serializble, bad things may happen.E:  Is this whole concept so fundamentally flawed/abhorrent/evil that it will blow up on me later on, and I should erase this code and sit in the corner for 5 minutes? [looksaround]Thanks in advance for any ideas or suggestions.EDIT:  sorry if this belongs in the beginners area   
  What about System.Collections.Generic.Dictionary<AbilityType, int> ?   ;Quote:Original post by bubu LVWhat about System.Collections.Generic.Dictionary<AbilityType, int> ?QFE.   ;  The extra a custom class could provide is restrictions on the generic's parameters.Technically speaking, these solutions are not arrays but dictionaries.   ;  Regarding point D, note that Dictionaries are not serializable. You'll have to implement the serialization logic yourself, by dumping the KeyValuePairs of the dictionary to an array.Also note that Dictionary<E, T> is more inefficient than it looks, especially if E does not implement IEquatable<>.However, for most uses your code should be fine. Just don't expect it to be a speed demon in tight loops.   ;Quote:    Original post by bubu LV    What about System.Collections.Generic.Dictionary<AbilityType, int> ?QFE.Hehe, yah, good question, I had a sinking suspicion I was overthinking the situation...but... (I just can't help myself)It does provide automatic initialization of the dictionary with the correct number of entries to the default value of whatever T is.  I know, I know, I'm probably only saving a few lines of constructor code somewhere...Quote: The extra a custom class could provide is restrictions on the generic's parameters.Technically speaking, these solutions are not arrays but dictionaries.Good points.Thanks!   ;  @Fiddler:Thanks, I didn't realize this about Dictionaries.  I never expected this method to be speedy, but I'll read up on this IEquatable<> thing anyway, sounds like it may be important later on.   ;  Although I'm probably going to ditch the whole idea above, and right now I'm probably beating a dead thread to death(I promise I'm not a clever spam-bot in disguise:), this little side-trek led to an interesting article over at Code Project:Accelerating Enum-Based DictionariesAnyone using a Dictionary<> with Enum keys should have a look, some clever (at least to me) stuff here about what Fiddler alluded to earlier.   