C# f泛型方法的限制条件

http://mgc.ahau.edu.cn/article.asp?id=616

C#中使用where子句限制泛型方法的泛型类型。
1.要求泛型类型实现一个接口或派生于某个基类;
2.不能定义必须由泛型类型实现的运算符。

TestMethodTTwo.cs:

[c-sharp] view plain copy
  1. using System;     
  2. using System.Collections.Generic;     
  3.     
  4. namespace Magci.Test.Collections     
  5. {     
  6.     //定义接口     
  7.     public interface IAccount     
  8.     {     
  9.         string Name     
  10.         {     
  11.             get;     
  12.         }     
  13.              
  14.         decimal Balance     
  15.         {     
  16.             get;     
  17.         }     
  18.     }     
  19.     
  20.     public class Account : IAccount     
  21.     {     
  22.         private string name;     
  23.         public string Name     
  24.         {     
  25.             get    
  26.             {     
  27.                 return name;     
  28.             }     
  29.         }     
  30.     
  31.         private decimal balance;     
  32.         public decimal Balance     
  33.         {     
  34.             get    
  35.             {     
  36.                 return balance;     
  37.             }     
  38.         }     
  39.     
  40.         public Account(string name, decimal balance)     
  41.         {     
  42.             this.name = name;     
  43.             this.balance = balance;     
  44.         }     
  45.     }     
  46.     
  47.     public class Algorithm     
  48.     {     
  49.         //声明泛型方法     
  50.         public static decimal Total<TAccount>(IEnumerable<TAccount> e)     
  51.             //使用where子句限制泛型类型     
  52.             where TAccount : IAccount     
  53.         {     
  54.             decimal total = 0;     
  55.             foreach (TAccount a in e)     
  56.             {     
  57.                 total += a.Balance;     
  58.             }     
  59.             return total;     
  60.         }     
  61.     }     
  62.     
  63.     public class TestMethodTTwo     
  64.     {     
  65.         public static void Main()     
  66.         {     
  67.             List<Account> accounts = new List<Account>();     
  68.             accounts.Add(new Account("Magci", 9999.99m));     
  69.             accounts.Add(new Account("Haha", 1241.33m));     
  70.             accounts.Add(new Account("Heihei", 1551.2m));     
  71.             accounts.Add(new Account("Kevin", 2643m));     
  72.     
  73.             //调用泛型方法     
  74.             decimal total = Algorithm.Total(accounts);     
  75.             Console.WriteLine("Total : {0:C}", total);     
  76.         }     
  77.     }     
  78. }   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值