泛型委托

来源:互联网 发布:软件项目风险管理 编辑:程序博客网 时间:2024/06/06 17:05
  1. #region Using directives
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. #endregion
  7. namespace GenericMethodDemo
  8. {
  9.   public static class Algorithm
  10.   {
  11.     //1.no operators with generics
  12.     //public static T Max<T>(T x, T y)
  13.     //{
  14.     //  if (x > y)
  15.     //    return x;
  16.     //  else
  17.     //    return y;
  18.     //}
  19.     //泛型方法Max;泛型委托Comparison<T>
  20.     //A
  21.     public static T Max<T>(T x, T y, Comparison<T> comparer)
  22.     {
  23.       int result = comparer(x, y);
  24.       return result > 0 ? x : y;
  25.     }
  26.     //面向接口编程,只要参数实现了IEnumerable接口即可
  27.     //B
  28.     public static decimal AccumulateSimple(IEnumerable e)
  29.     {
  30.       decimal sum = 0;
  31.       foreach (Account a in e)
  32.       {
  33.         sum += a.Balance;
  34.       }
  35.       return sum;
  36.     }
  37.     //泛型方法Accumulate
  38.     //C
  39.     public static decimal Accumulate<T, U>(U coll)
  40.       where U : IEnumerable<T>
  41.       where T : IAccount
  42.     {
  43.       decimal sum = 0;
  44.       foreach (T a in coll)
  45.       {
  46.         sum += a.Balance;
  47.       }
  48.       return sum;
  49.     }
  50.     //泛型方法Accumulate
  51.     //D
  52.     public static decimal Accumulate<TAccount>(IEnumerable<TAccount> coll)
  53.       where TAccount : IAccount
  54.     {
  55.       decimal sum = 0;
  56.       foreach (TAccount a in coll)
  57.       {
  58.         sum += a.Balance;
  59.       }
  60.       return sum;
  61.     }
  62.     //定义委托Adder
  63.     public delegate U Adder<T, U>(T t, U u);
  64.     //泛型方法Accumulate;泛型委托Adder
  65.     //E
  66.     public static U Accumulate<T, U>(IEnumerable<T> coll, Adder<T, U> adder)
  67.     {
  68.       U sum = default(U);
  69.       foreach (T a in coll)
  70.       {
  71.         sum = adder(a, sum);
  72.       }
  73.       return sum;
  74.     }
  75.     //泛型方法Accumulate;泛型委托Adder,Predicate
  76.     //F
  77.     public static U AccumulateIf<T, U>(IEnumerable<T> coll, Adder<T, U> adder, Predicate<T> match)
  78.     {
  79.       U sum = default(U);
  80.       foreach (T a in coll)
  81.       {
  82.         if (match(a))
  83.         {
  84.           sum = adder(a, sum);
  85.         }
  86.       }
  87.       return sum;
  88.     }
  89.   }
  90.   
  91.   public interface IAccount
  92.   {
  93.     decimal Balance { get; }
  94.     string Name { get; }
  95.   }  
  96.   
  97.   public class Account : IAccount
  98.   {
  99.     private string name;
  100.     public string Name
  101.     {
  102.       get
  103.       {
  104.         return name;
  105.       }
  106.     }
  107.     private decimal balance;
  108.     public decimal Balance
  109.     {
  110.       get
  111.       {
  112.         return balance;
  113.       }
  114.     }
  115.     public Account(string name, Decimal balance)
  116.     {
  117.       this.name = name;
  118.       this.balance = balance;
  119.     }
  120.   }
  121.   class Program
  122.   {
  123.     static int Compare(int x, int y)
  124.     {
  125.       if (x == y)
  126.         return 0;
  127.       else if (x < y)
  128.         return -1;
  129.       else
  130.         return 1;
  131.     }
  132.     static decimal AccountAdder(Account a, decimal d)
  133.     {
  134.       return a.Balance + d;
  135.     }
  136.     static void Main(string[] args)
  137.     {
  138.       int i = 3;
  139.       int j = 4;
  140.       //A
  141.       int result = Algorithm.Max<int>(i, j, new Comparison<int>(Program.Compare));
  142.       List<Account> accounts = new List<Account>();
  143.       accounts.Add(new Account("Christian", 1500));
  144.       accounts.Add(new Account("Sharon", 2200));
  145.       accounts.Add(new Account("Katie", 1800));
  146.       foreach (Account a in accounts)
  147.       {
  148.         Console.WriteLine(a.Name);
  149.       }
  150.       //B
  151.       decimal sum1 = Algorithm.AccumulateSimple(accounts);
  152.       Console.WriteLine(sum1);
  153.       //D
  154.       decimal sum2 = Algorithm.Accumulate(accounts);
  155.       Console.WriteLine("sum of all accounts {0}", Algorithm.Accumulate<Account>(accounts));
  156.       
  157.       
  158.       //C
  159.       decimal sum_test = Algorithm.Accumulate<Account, List<Account>>(accounts);      
  160.       Console.WriteLine(sum_test);
  161.       
  162.       //E
  163.       decimal sum3 = Algorithm.Accumulate<Account, decimal>(accounts, new Algorithm.Adder<Account, decimal>(AccountAdder));
  164.       Console.WriteLine(sum3);
  165.       //E
  166.       decimal sum3b = Algorithm.Accumulate<Account, decimal>(accounts, AccountAdder);
  167.       Console.WriteLine("3b " + sum3b);
  168.       //E
  169.       decimal sum4 = Algorithm.Accumulate<Account, decimal>(accounts,
  170.         delegate(Account a, decimal d) { return a.Balance + d; });
  171.       Console.WriteLine(sum4);
  172.       //F
  173.       decimal sum5 = Algorithm.AccumulateIf<Account, decimal>(
  174.         accounts,
  175.         delegate(Account a, decimal d) { return a.Balance + d; },
  176.         delegate(Account a) { return a.Balance > 1800 ? true : false; });
  177.       Console.WriteLine(sum5);      
  178.     }
  179.   }
  180. }

原创粉丝点击