在以下代码的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)