Lazy Evaluation

 https://stackoverflow.com/questions/2515796/deferred-execution-and-eager-evaluation

 public
class LazyEvaluation { public int Count { get; set; } public int Computation(int index) { this.Count++; if (index == 0) { return 0; } else if (index == 1) { return 1; } else if (index >= 2) { return Computation(index - 2) + Computation(index - 1); } else { throw new ArgumentException("index"); } } public IEnumerable<int> GetComputationEager(int maxIndex) { var result = new int[maxIndex]; for (int i = 0; i < maxIndex; i++) { result[i] = this.Computation(i); } foreach (var value in result) { yield return value; } } public IEnumerable<int> GetComputationImmediate(int maxIndex) { var result = new int[maxIndex]; for (int i = 0; i < maxIndex; i++) { result[i] = this.Computation(i); } return result; } public IEnumerable<int> GetComputationLazy(int maxIndex) { for (int i = 0; i < maxIndex; i++) { yield return this.Computation(i); } } }
原文地址:https://www.cnblogs.com/ryan-y-an/p/8513926.html