我正在寻找正确的集合类型以在 C# 中实现此结果。我想从我预先指定的几个枚举字段创建一个项目列表。
- 项目类型
- 项目 Material
对于我添加的每个项目类型,我希望创建一组遵循此一般模式的新项目(从 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
如果有人知道实现此类目标的最佳数据类型,或者可能是允许您迭代两者组合的正确方法,我将非常感激。
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
}
最佳答案
不需要特定的集合。只需声明项目类并创建所有排列即可。
如果您有快速访问、快速迭代、一项指向另一项等特定要求,则需要选择一个集合。
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;
}
}
由于这是一个枚举,并且无法动态创建,因此在启动时创建所有可能的组合:
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));
}
}
关于c# - 用于获取 2 个不同枚举的排列的最佳集合类型是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63761846/