You can create an interface and class es that implement it:
public interface IPriceMethod { double Calculate(IList<double> priceHistorie); } public class AveragePrice : IPriceMethod { public double Calculate(IList<double> priceHistorie) { return priceHistorie.Average(); } }
Edit: another method uses Dictionary to display Func s, so you don't need to create classes just for that (this code is based on Servy code, which has since deleted its answer):
public class Article { private static readonly Dictionary<Pricemethod, Func<IEnumerable<double>, double>> priceMethods = new Dictionary<Pricemethod, Func<IEnumerable<double>, double>> { {Pricemethod.Max,ph => ph.Max()}, {Pricemethod.Min,ph => ph.Min()}, {Pricemethod.Average,ph => ph.Average()}, }; public Pricemethod Pricemethod { get; set; } public List<Double> Pricehistory { get; set; } public double Price { get { return priceMethods[Pricemethod](Pricehistory); } } }
source share