public interface ITest { decimal Balance { get; } }
public class Test : ITest { public Test(decimal balance) { this._balance = balance; } private decimal _balance; public decimal Balance { get { return _balance; } } }
class Program { static void Main(string[] args) { List<Test> list = new List<Test>() { new Test(23), new Test(5) }; decimal amount = Accumulate<Test, decimal>(list, (item, sum) => sum += item.Balance); Console.WriteLine(amount); Console.ReadKey(); } public static T2 Accumulate<T1, T2>(List<T1> source, Func<T1, T2, T2> action) where T1 : ITest where T2 : struct { T2 sum = default(T2); foreach (T1 item in source) { sum = action(item, sum); } return sum; } }
时间: 2024-10-12 08:37:25