首页 > 其他 > 详细

委托的使用1

时间:2015-03-18 20:01:10      阅读:346      评论:0      收藏:0      [点我收藏+]

namespace DelegateTest
{
    delegate bool FilterDelegate(int i); //声明委托,参数类型,返回类型,委托名称。
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 5, 6, 6, 7, 8, 9 };
            List<int> newList = MyFilter(array,FilterOdd);  //传入委托函数
            foreach (int item in newList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

        }
        static List<int> MyFilter(int[] array, FilterDelegate filter) //把委托函数当做参数,委托类型
        {
            List<int> list = new List<int>();
            for (int i = 0; i < array.Length; i++)
            {
                if (filter(i))  //调用委托函数
                {
                    list.Add(i);
                }
            }
            return list;
        }

        static bool FilterOdd(int i)  //委托必须是static方法吗?
        {
            return i % 2 == 1;
        }
    }
}

委托的使用1

原文:http://www.cnblogs.com/ronye/p/4348085.html

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