【问题标题】:C# Save from listC# 从列表中保存
【发布时间】:2016-04-19 08:39:20
【问题描述】:

您好,我有一个名为 Animal.cs 的类,在我的 Program.cs 中我创建了一些动物..

class Program
    {
        static void Main(string[] args)
        {
            var AnimalList = new AnimslList();
        animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
        animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
        animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
        }
     }

我有一个类 AnimalList.cs,在那个类中我添加了动物列表,我用这个代码得到了活跃的动物:

class AnimalList : List<Animal>
{
    public List<Animal> GetActiveAnimals()
    {
        return this.FindAll(p => p.Active);
    }
}

我的问题是在这个类中,他在 event.cs 中调用了 Event.cs 我想从 GetActiveAnimals() 中获取结果,因为我只想获取活动的活动动物,有人可以帮我吗?

class Event
    {
        private AnimalList _contestants;
    }

我应该从上面的 AnimaList 中获取 Active。 活跃的应该进入_contestants

【问题讨论】:

  • 你和@Tiago在做同一个项目吗?
  • 您需要向我们提供更相关的代码以及您在这里面临的实际问题 - what 事件,对于初学者来说?
  • @dotctor 不,但我会给他帮助我有他的解决方案

标签: c# list class


【解决方案1】:

例如通过构造函数扩展您的事件

class Event
{
    private AnimalList _contestants;
    public Event(AnimalList contestants)
    { 
         _contestants = contestants;
    }
}

然后在创建事件时转发列表:

static void Main(string[] args)
{
    var AnimalList = new AnimslList();
    animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
    animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
    animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
    var event = new Event(AnimalList.GetActiveAnimals();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多