簡體   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