首页 > 其他 > 详细

带约束都泛型方法

时间:2015-08-26 22:05:02      阅读:173      评论:0      收藏:0      [点我收藏+]
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;
        }
    }

 

带约束都泛型方法

原文:http://www.cnblogs.com/webczw/p/4761743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!