相关疑难解决方法(0)

是否可以缓存lambda表达式中计算的值?

在以下代码的ContainsIngredients方法中,是否可以缓存p.Ingredients值而不是多次显式引用它?这是我刚熟了用于说明目的相当简单的例子,但我工作的引用代码值深处p如.p.InnerObject.ExpensiveMethod().值

编辑:我正在使用来自http://www.albahari.com/nutshell/predicatebuilder.html的PredicateBuilder

public class IngredientBag
{
    private readonly Dictionary<string, string> _ingredients = new Dictionary<string, string>();

    public void Add(string type, string name)
    {
        _ingredients.Add(type, name);
    }

    public string Get(string type)
    {
        return _ingredients[type];
    }

    public bool Contains(string type)
    {
        return _ingredients.ContainsKey(type);
    }
}

public class Potion
{
    public IngredientBag Ingredients { get; private set;}
    public string Name {get; private set;}        

    public Potion(string name) : this(name, null)
    {

    }

    public Potion(string name, IngredientBag ingredients)
    { …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda predicate

4
推荐指数
1
解决办法
3633
查看次数

标签 统计

c# ×1

lambda ×1

linq ×1

predicate ×1