private static void 泛型方法() { //普通泛型方法 var accounts = new List <Account>() { new Account("Christian", 1500), new Account("Stephanie", 2200), new Account("Angela", 1800), new Account("Matthias", 2400) }; WriteLine("普通泛型方法"); decimal balance1 = Algorithms.AccumulateSimple(accounts); WriteLine(balance1); //带约束的泛型方法 var accounts1 = new List <IAccount>() { new Account1("Christian", 1500), new Account1("Stephanie", 2200), new Account1("Angela", 1800), new Account1("Matthias", 2400) }; WriteLine("带约束的泛型方法"); decimal balance2 = Algorithms.AccumulateSimple1 <IAccount>(accounts1); decimal balance3 = Algorithms.AccumulateSimple1(accounts1);//编译器会从方法的参数类型中自动推断出泛型类型参数 这两种调用方式是一样的 WriteLine(balance2); WriteLine("带委托的泛型方法"); var accounts3 = new List <Account>() { new Account("Christian", 1500), new Account("Stephanie", 2200), new Account("Angela", 1800), new Account("Matthias", 2400) }; decimal amount = Algorithms.AccumulateSimple2 <Account, decimal>(accounts3, (account, sum) => sum += account.Balance); }