zoukankan      html  css  js  c++  java
  • 带委托并且带约束的泛型方法

        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;
            }
        }
  • 相关阅读:
    java中的设计模式
    stack
    最大堆排序
    Starship Troopers
    Tick and Tick
    Last non-zero Digit in N!
    G
    C
    B
    A
  • 原文地址:https://www.cnblogs.com/webczw/p/4761797.html
Copyright © 2011-2022 走看看