Search code examples
c#listenumsiterator

What is the best Collection Type to use to get the permutations of 2 different enums


I am seeking out the right collection type to achieve this result in C#. I am wanting to create a List of Items, from a couple of enum fields I've pre-specified.

  • itemType
  • itemMaterial

For every itemType I add, I would like for a new set of items to be created that follows this general pattern, (ripped from Runescape for ease of concept conveyance, don't come after me JaGex):

ItemType enum:
Helmet, Platebody, Platelegs, Boots

ItemMaterial enum: 
Bronze, Iron, Steel, Black, White, Mithril, Adamant, Rune, Dragon...

Items List(?):
    BronzeHelmet, IronHelmet, SteelHelmet, BlackHelmet, WhiteHelmet, MithrilHelmet, AdamantHelmet, RuneHelmet, DragonHelmet
    BronzePlatebody, IronPlatebody, SteelPlatebody, BlackPlatebody, WhitePlatebody, MithrilPlatebody, AdamantPlatebody, RunePlatebody, DragonPlatebody
    BronzePlatelegs, IronPlatelegs, SteelPlatelegs, BlackPlatelegs, WhitePlatelegs, MithrilPlatelegs, AdamantPlatelegs, RunePlatelegs, DragonPlatelegs
    BronzeBoots, IronBoots, SteelBoots, BlackBoots, WhiteBoots, MithrilBoots, AdamantBoots, RuneBoots, DragonBoots

If anybody knows the best data type to achieve something like this with, or maybe the right methods for it that allow you to iterate the combination of the two, I would appreciate it so much.

enum ItemType
{
    FullHelmet,
    MediumHelmet,
    Platebody,
    Chainbody,
    Platelegs,
    Plateskirt,
    Boots,
    Shoes,
    KiteShield,
    SquareShield,
    Battleaxe,
    WarHammer,
    Longsword,
    Shortsword,
    Dagger,
    Longbow,
    Shortbow,
    Crossbow,
    LongStaff,
    Staff,
    Wand
}

enum ItemQuality
{
    Wood,
    Bronze,
    Iron,   
    Steel,
    Black,
    Mithril,
    Adamant,
    Rune,
    Dragon,
    Barrows,
    GodWars,
    Elite
}

Solution

  • No need for a specific collection. Just declare the item class and create all permutations.

    Choosing a collection would be needed if you had specific requirements like quick access, fast iterations, one item pointing to another and more.

    public class Item {
        public ItemType Type { get; set; }
        public ItemQuality Material { get; set; }
    
        public Item(ItemType type, ItemQuality material) {
            this.Type = type;
            this.Material = material;
        }
    }
    

    Since this is an enum, and it can't be dynamically created, in your startup create all the possible combinations:

    List<Item> Items;
    var types = Enum.GetValues(typeof(ItemType));
    var materials = Enum.GetValues(typeof(ItemQuality));
    foreach(ItemType type in types) {
        foreach(ItemQuality material in materials) {
            Items.Add(new Item(type, material));
        }
    }