简体   繁体   English

用于获取 2 个不同枚举的排列的最佳集合类型是什么

[英]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#.我正在寻找正确的集合类型以在 C# 中实现此结果。 I am wanting to create a List of Items, from a couple of enum fields I've pre-specified.我想从我预先指定的几个枚举字段创建一个项目列表。

  • itemType物品种类
  • itemMaterial item材质

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,我希望创建一组遵循此一般模式的新项目,(为了便于概念传达,从 Runescape 中提取,不要来找我 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
}

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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM