Jacklovely

导航

 

说明(2017-11-23 10:46:33):

1. Func有返回值,Action无返回值,以后就不用定义delegate委托了。

2. 不过还是不知道什么时候该用委托,蒋坤在讲完事件后,留了个作业,就是经典的窗体传值,试试看能不能自己做出来。

使用Func求水仙花数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _00_Test
{

    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            for (int i = 100; i < 1000; i++)
            {
                list.Add(i);
            }
            int[] nums = MyWhere(list.ToArray(), e =>
            {
                int n1 = e % 10;
                int n2 = e / 10 % 10;
                int n3 = e / 100;
                return n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3 == e;
            });

        }
        //Func有返回值,Action无返回值,以后就不用定义delegate委托了。
        public static int[] MyWhere(int[] nums, Func<int, bool> Foo)
        {
            List<int> list = new List<int>();
            foreach (int n in nums)
            {
                if (Foo(n))
                {
                    list.Add(n);
                }
            }
            return list.ToArray();
        }
    }
}

 

posted on 2017-11-23 10:48  Jacklovely  阅读(331)  评论(0编辑  收藏  举报