泛型方法

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace Generic_Method
  6. {
  7.   /*
  8.   //-------第一种方案--普通方法---------
  9.   public class Account
  10.   {
  11.     private string name;
  12.     public string Name
  13.     {
  14.       get { return name; }
  15.     }
  16.     private decimal balance;
  17.     public decimal Balance
  18.     {
  19.       get { return balance; }
  20.     }
  21.     public Account(string name, decimal balance)
  22.     {
  23.       this.balance = balance;
  24.       this.name = name;
  25.     }
  26.   }
  27.   public static class Algorithm
  28.   {
  29.     //面对接口编程,只要参数实现了IEnumerable接口就行;
  30.     //比如Main函数里的List<Account> accounts;
  31.     public static decimal AccumulateSimple(IEnumerable e) 
  32.     {
  33.       decimal sum = 0;
  34.       foreach (Account a in e)
  35.       {
  36.         sum += a.Balance;
  37.       }
  38.       return sum;
  39.     }
  40.   }
  41.   class Program
  42.   {
  43.     static void Main(string[] args)
  44.     {
  45.       List<Account> accounts = new List<Account>();
  46.       accounts.Add(new Account("a", 1500));
  47.       accounts.Add(new Account("b", 2000));
  48.       accounts.Add(new Account("c", 2500));
  49.       decimal amount = Algorithm.AccumulateSimple(accounts);
  50.       Console.WriteLine(amount);
  51.     }
  52.   }
  53.   */
  54.   //-------第二种方案--使用泛型编码---------
  55.   public interface IAccount
  56.   {
  57.     string Name
  58.     { get;}
  59.     decimal Balance
  60.     { get;}
  61.   }
  62.   public class Account : IAccount
  63.   {
  64.     private string name;
  65.     public string Name
  66.     {
  67.       get { return name; }
  68.     }
  69.     private decimal balance;
  70.     public decimal Balance
  71.     {
  72.       get { return balance; }
  73.     }
  74.     public Account(string name, decimal balance)
  75.     {
  76.       this.balance = balance;
  77.       this.name = name;
  78.     }
  79.   }
  80.   public static class Algorithm
  81.   {
  82.     //面对接口编程,只要参数实现了IEnumerable接口就行;
  83.     //比如Main函数里的List<Account> accounts;
  84.     //where 子句可以用于泛型类也可以用于泛型方法
  85.     public static decimal AccumulateSimple<TAccount> (IEnumerable<TAccount> coll)
  86.       where TAccount:IAccount
  87.     {
  88.       decimal sum = 0;
  89.       foreach (TAccount a in coll)
  90.       {
  91.         sum += a.Balance;
  92.       }
  93.       return sum;
  94.     }
  95.   }
  96.   class Program
  97.   {
  98.     static void Main(string[] args)
  99.     {
  100.       List<Account> accounts = new List<Account>();
  101.       accounts.Add(new Account("a", 1500));
  102.       accounts.Add(new Account("b", 2000));
  103.       accounts.Add(new Account("c", 2500));
  104.       decimal amount = Algorithm.AccumulateSimple(accounts);
  105.       Console.WriteLine(amount);
  106.     }
  107.   }
  108. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值