c#Func简单使用

来源:互联网 发布:数据挖掘pdf 编辑:程序博客网 时间:2024/05/17 07:31
        private Func<string, string> TestFun()        {            Func<string, string> fun = k => k.ToUpper();            return fun;        }        private Func<string, string, bool> HandderCondition()        {            Func<string, string, bool> kk = (x, w) => x.Length > 5 && x.Contains(w);            return kk;        }

string[] arr = { "orange", "apple", "Article", "elephant", "star", "and" };            string name = "Dakota";            Response.Write(TestFun()(name) + "<br />");            //            var query = arr.Where(x => HandderCondition()(x, "a")).Select(t => t);            foreach (var item in query)            {                Response.Write(item + "<br />");            }

输出:

DAKOTA
orange
elephant

 Func<double, double> myfunc = (x) => 2.0 * x * x - 0.5 * x;            Console.WriteLine(myfunc(1)); //1.5            //or            Console.WriteLine((myfunc = (x) => 2.0 * x * x - 0.5 * x)(1)); //1.5            //阶层函数            Func<Func<int, int>, Func<int, int>> F = factorial => n => n == 0 ? 1 : n * factorial(n - 1);            //一个参数的括号可以省略            //Func<int, Func<int>> fa = (x) => () => x * 3;            Func<int, Func<int>> fa = x => () => x * 3;            //Func<int, Func<int, int>> fb = (x) => (y) => x * y;            Func<int, Func<int, int>> fb = x => y => x * y;            Console.WriteLine("fa:{0}, fb:{1}", fa(10)(), fb(10)(5));    //30,50



0 0