【问题标题】:How do I check if delegate returns true inside of a method?如何检查委托是否在方法内返回 true?
【发布时间】:2021-05-08 07:32:11
【问题描述】:

我正在尝试解决考试中的一项任务,但我遇到了代表问题。该任务要求我为FoodTraker 类定义一个方法,该方法将Func<Portion,bool> 类型的委托作为参数,并从列表中删除满足委托标准的所有部分。同样在主要测试代码中,通过给方法提供一个参数,该方法将过滤所有价格大于每重量 30 的部分。

public class Portion
    {
        public Portion(decimal price, double weight, double kcal)
        {
            Price = price;
            Weight = weight;
            Kcal = kcal;
        }
    public decimal Price { get; private set; }
    public double Weight { get; private set; }
    public double Kcal { get; private set; }


    }





public class FoodTracker
    {
        public List<Portion> portions;

        public Func<Portion, bool> PortionDelegate;
        

        public FoodTracker()
        {
            portions = new List<Portion>();
        }
    public void Track(Portion portion)
        {
            portions.Add(portion);
        }

    public virtual Decimal CalculateProfit()
        {
            Decimal sum = 0;
            foreach(var portion in portions)
            {
                sum += portion.Price;
            }
            return sum;
        }

    public void PortionRemover(Func<Portion,bool> portionDelegate)
        {
           portions.Remove(portion);
        }
    }


public class Program
    {
        
        
        static void Main(string[] args)
        {
            Portion portion1 = new Portion(10m, 500, 1000);
            Portion portion2 = new Portion(20m, 200, 200);
            Portion portion3 = new Portion(5m, 50, 500);
            Portion portion4 = new Portion(30m,1000, 700);
            NutritionalTracker tracker = new NutritionalTracker(699, 501);
            List<Portion> portions = new List<Portion>();
            tracker.Track(portion1);
            tracker.Track(portion2);
            tracker.Track(portion3);
            tracker.Track(portion4);
            portions.Add(portion1);
            portions.Add(portion2);
            portions.Add(portion3);
            portions.Add(portion4);
             
           
            



        Console.WriteLine(tracker.CalculateProfit());

        }

我的第一个问题是我不知道如何检查方法内委托的布尔条件,这导致我无法完成其余任务。

【问题讨论】:

  • var result = portionDelegate.Invoke(portion);var result = portionDelegate(portion);
  • portions.RemoveAll(portionDelegate);?不清楚你真正想要的是哪个函数/代码行。
  • @Andy 我不确定这对我有什么帮助,也许我只是妄想或者你不明白我的问题。另外 Charlieface 我从不打算使用 RemoveAll。我首先做的是foreach(var portion in portions) { if(PortionDelegate == true) { portions.Remove(portion)} } ,但这显然不正确。

标签: c# methods delegates


【解决方案1】:

你可以这样删除

在 using 块上添加 Linq 扩展功能;

using System.Linq;

更改过滤列表的方法并设置它。

public void PortionRemover(Func<Portion, bool> portionDelegate)
    {
        portions = portions.Where(portionDelegate).ToList();
    }

调用示例

 FoodTracker tracker = new FoodTracker();
 tracker.PortionRemover(p => p.Price > 30);

【讨论】:

  • 尽管我不怀疑解决方案的正确性,但我认为我不允许在该任务中使用 Linq,因此如果可能的话,最好使用不使用它的解决方案。另外我需要过滤它们按价格/重量 > 30。
猜你喜欢
  • 2012-02-10
  • 2018-09-29
  • 2011-03-13
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多