swollaws
漂泊中体会不到生活的味道,那是因为吃不到老妈烧的饭。

一、简单知识和演示

  1. public void page_load(object sender, EventArgs e)
  2. {
  3.     //把泛型类型赋予方法调用
  4.     int x = 4;
  5.     int y = 5;
  6.     Check<int>(ref x, ref y);
  7.  
  8.     //由于C#编译器会通过调用Text方法来获取参数的
  9.     //类型,所以也可以不把泛型类型赋予方法调用。
  10.     //即泛型方法可以像非泛型方法那样调用
  11.     Check(ref x, ref y);
  12.  
  13.     Response.Write(x + "--" + y);
  14. }
  15.  
  16. //在泛型方法中,泛型类型用方法声明来定义
  17. public void Check<T>(ref T x, ref T y)
  18. {
  19.     T temp = x;
  20.     x = y;
  21.     y = temp;
  22. }

 

二、泛型方法和委托实例

  1. namespace GenericMethodDemo
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.  
  7.     public interface IAccount
  8.     {
  9.         string Name { get; }
  10.         decimal Balance { get; }
  11.     }
  12.  
  13.     public class Account : IAccount
  14.     {
  15.         private string name;
  16.         public string Name
  17.         {
  18.             get { return name; }
  19.         }
  20.  
  21.         private decimal balance;
  22.         public decimal Balance
  23.         {
  24.             get { return balance; }
  25.         }
  26.  
  27.         public Account(string name, Decimal balance)
  28.         {
  29.             this.name = name;
  30.             this.balance = balance;
  31.         }
  32.     }
  33.  
  34.     public static class Algorithm
  35.     {
  36.         //一般的方法【参数为继承IEnumerable接口的类】
  37.         public static decimal AccumulateSimple(IEnumerable e)
  38.         {
  39.             decimal sum = 0;
  40.  
  41.             foreach (Account a in e)
  42.             {
  43.                 sum += a.Balance;
  44.             }//这里使用Account显示出一定局限性
  45.  
  46.             return sum;
  47.         }
  48.  
  49.         //一般的泛型方法【参数为继承IEnumerable<T>泛型接口的类】
  50.         public static decimal Accumulate<T>(IEnumerable<T> coll)
  51.             where T : IAccount//接口约束--任然显示出一定得局限性,下面使用委托可以克服这种局限
  52.         {
  53.             decimal sum = 0;
  54.  
  55.             foreach (T a in coll)
  56.             {
  57.                 sum += a.Balance;
  58.             }
  59.  
  60.             return sum;
  61.         }
  62.  
  63.         //泛型委托
  64.         public delegate U Adder<T, U>(T t, U u);
  65.         //使用泛型委托的泛型方法
  66.         public static U Accumulate<T, U>(IEnumerable<T> coll, Adder<T, U> adder)
  67.         {
  68.             U sum = default(U);//设置默认值
  69.  
  70.             foreach (T a in coll)
  71.             {
  72.                 sum = adder(a, sum);
  73.             }
  74.  
  75.             return sum;
  76.         }
  77.         public static U AccumulateIf<T, U>(IEnumerable<T> coll, Adder<T, U> adder, Predicate<T> match)
  78.         {
  79.             U sum = default(U);
  80.            
  81.             foreach (T a in coll)
  82.             {
  83.                 if (match(a))
  84.                     sum = adder(a, sum);
  85.             }
  86.  
  87.             return sum;
  88.         }
  89.     }
  90.  
  91.     class Program
  92.     {
  93.         static void Main(string[] args)
  94.         {
  95.             List<Account> accounts = new List<Account>();
  96.             accounts.Add(new Account("Christian", 1500));
  97.             accounts.Add(new Account("Sharon", 2200));
  98.             accounts.Add(new Account("Katie", 1800));
  99.  
  100.             //调用一般的方法
  101.             Console.WriteLine("sum of all accounts {0}", Algorithm.AccumulateSimple(accounts));
  102.             //调用一般的泛型方法
  103.             Console.WriteLine("sum of all accounts {0}", Algorithm.Accumulate<Account>(accounts));
  104.             //调用使用泛型委托的泛型方法
  105.             Console.WriteLine("sum of all accounts {0}",
  106.                 Algorithm.Accumulate<Account, decimal>(accounts, delegate(Account a, decimal d) { return a.Balance + d; }));//使用匿名委托
  107.             Console.WriteLine("sum of all accounts {0}",
  108.                 Algorithm.Accumulate<Account, decimal>(accounts, new Algorithm.Adder<Account, decimal>(AccountAdder)));//使用泛型委托
  109.             Console.WriteLine("sum of all accounts {0}",
  110.                 Algorithm.Accumulate<Account, decimal>(accounts, AccountAdder));
  111.  
  112.             Console.WriteLine(Algorithm.AccumulateIf<Account, decimal>(
  113.                 accounts,
  114.                 delegate(Account a, decimal d) { return a.Balance + d; },
  115.                 delegate(Account a) { return a.Balance > 1800 ? true : false; }
  116.                 ));//使用匿名委托
  117.  
  118.             Console.ReadLine();
  119.         }
  120.  
  121.         //委托调用方法
  122.         static decimal AccountAdder(Account a, decimal d)
  123.         {
  124.             return a.Balance + d;
  125.         }
  126.     }
  127. }
posted on 2009-05-13 13:04  swollaw  阅读(399)  评论(0编辑  收藏  举报