繁体   English   中英

如何从通用列表中删除基于类与构造函数的项目?

[英]How to remove from Generic List an item based on class with constructor?

搜索了很多,但我无法弄清楚。 我有一些属性的甲级盔甲。 我能够添加它,并且我通过功能添加了排序功能。

我上课了:

public class Item : IComparable<Item>
{
    public string name, description;
    public bool stackable;
    public int value;
    public string coin_type;
    public int weight;
    //  public string model_path;

    public Item (string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type, int c_weight)
    {
        name = c_name;
        description = c_description;
        stackable = c_stackable;
        value = c_value;
        coin_type = c_coin_type;
        weight = c_weight;
        //  model_path = c_model_path;
        //, string c_model_path)
    }
    public int CompareTo(Item other)
    {
        return String.Compare (name, other.name); // a < ab < b (sortowanie wg. litery)
    }
}

然后,我有一个“孩子”类,它是从Item类派生的。

public class Armor : Item, IComparable <Armor>
{
    public string armor_prof;
    public string armor_category;
    public int armor_class;
    public int armor_str_req;
    public string armor_stealth_mod;
    //public string armor_property;
    public Armor (string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type, int c_weight, string c_armor_prof, string c_armor_category, int c_armor_class, int c_armor_str_req, string c_armor_stealth_mod) : base (c_name, c_description, c_stackable, c_value, c_coin_type, c_weight)
    {
        armor_prof = c_armor_prof;
        armor_category = c_armor_category;
        armor_class = c_armor_class;
        armor_str_req = c_armor_str_req;
        armor_stealth_mod = c_armor_stealth_mod;
    //  armor_property = c_armor_property;
    }

这是添加项目并对其进行排序的功能:

    public void AddToCharacterInventory(Item it)
    {
        if (it is Weapon)
        {
            charInvWeapon.Add((Weapon)it);
            charInvWeapon.Sort();
        }
        else if (it is Armor)
        {
            charInvArmor.Add((Armor)it);
            charInvArmor.Sort();
        }
}

然后我添加项目:

AddToCharacterInventory(new Armor("Leather armor", "Description.", false, 10, "gp", 10, "Leather", "Light Armor", 11, 0, ""));

它被添加到名为charInvArmor的列表中。 现在,如何根据某些属性将其删除? 例如,名称为“皮革装甲”。 因此,这是公共字符串名称,该类盔甲从类项目继承。 如果它是一个简单的字符串,很容易从列表中删除位置,但是当type是具有属性的类时,如何在这种情况下删除位置呢?

考虑下面的例子,它应该指导您正确的方向:)

static void Main()
{
    var items = new List<Item>()
    {
        new Item("Axe", true),
        new Item("Armor", false),
        new Item("Horse", false)
    };

    var remove = items.FirstOrDefault(item =>
    {
        return item.Name.Equals("Armor", StringComparison.InvariantCultureIgnoreCase) &&
               !item.Active;
    });

    if (remove != null)
        items.Remove(remove);

    Console.WriteLine(string.Join(", ", items.Select(item => item.Name)));
    Console.ReadLine();
}

public class Item
{
    public Item(string name, bool active)
    {
        Name = name;
        Active = active;
    }

    public string Name { get; set; }

    public bool Active { get; set; }
}

例

您有不同的方法来做到这一点。 一种方法是找到对象,然后将其删除,如下所示:

var item= charInvArmor.First(c=>c.Name =="something");
charInvArmor.Remove(item);

另一种方法是实现IEquatable( 请参阅这篇文章

也可以使用RemoveAt,但是我自己通常使用method1

暂无
暂无

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

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