public interface IAccount { decimal Balance { get; set; } string Name { get; } }
public class Account : IAccount { private decimal balance; public decimal Balance { get { return balance; } set { balance = value; } } public string Name { get { throw new NotImplementedException(); } } }
private decimal _balance; public decimal Balance { get { return _balance; } set { _balance = value; } } public string Name { get { throw new NotImplementedException(); } } }
class Program { static void Main(string[] args) { List<Account> list = new List<Account>() { new Account() { Balance = 5 }, new Account() { Balance = 5 } }; decimal retvalue = Accumulate<Account>(list); Console.WriteLine(retvalue); List<Bccount> list1 = new List<Bccount>() { new Bccount() { Balance = 8 }, new Bccount() { Balance = 6 } }; decimal retvalue1 = Accumulate<Bccount>(list1); Console.WriteLine(retvalue1); Console.ReadKey(); } public static decimal Accumulate<T>(List<T> source) where T : IAccount { decimal sum = 0; foreach (T a in source) { sum += a.Balance; } return sum; } }
时间: 2024-10-10 21:32:20