csharp Lambda Expression(Basic ++)。cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Lambda Expression(Basic ++)。cs相关的知识,希望对你有一定的参考价值。

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

namespace Lambda_Expression
{
    class Book
    {
        public string Author { get; set; }
        public string Name { get; set; }
        public DateTime Published { get; set; }
    }
	
    class Program
    {
        static void Main(string[] args)
        {
            /*==================================== LAMBDA EXPRESSION (Functional Programming)==================================*/
            //EXAMPLE:1
            Func<int, int> func1 = x => x*2; //function defination: Func<returnType, param_types_one_by_one> functionName = (parameters_one_by_one) => expression/code_snippet;
            Console.WriteLine(func1.Invoke(5)); //invoking or calling function

            Func<double, int, double> func2 = (x, y) => 2*(x + y);
            Console.WriteLine(func2.Invoke(5,10));

            Func<int, int, int, int> func3 = (i, j, k) =>
            {
                int temp = 2*(i + j);
                return temp + k;
            };
            Console.WriteLine(func3.Invoke(5,10,20));
            Console.WriteLine();

            //EXAMPLE:2
            List<string> names=new List<string>() { "Kawser", "Rasel", "Moshiur"};
            string found = names.Find(n => n.StartsWith("R")); //snippets inside Find() method is lambda expression (NOTE: Find method want Predicate Type which is a lambda expression in fact
            //found = names.Find(n => n == "Moshiur");
            //Console.WriteLine(names.Exists(n => n=="Kawser"));
            Console.WriteLine(found);


            //EXAMPLE:3
            List<double> values=new List<double>() {30,40,60,70,80,90,100};
            double aNumber = values.Find(number => (number >= 50)); //finds the first occurrence of a number which is >=50 from the values list
            Console.WriteLine(aNumber);

            List<Book> bookList=new List<Book>()
            {
                new Book() { Author = "Humayun Ahmed", Name = "Captain-Q & Stranger from Unknown", Published = new DateTime(1993,05,20)},
                new Book() { Author = "Taslima Nasrin" , Name = "Amar Meyebela", Published = new DateTime(1996,06,15)},
                new Book() { Author = "Kaji Najrul", Name = "Bidrohi Kobita", Published = new DateTime(1995,12,17)}
            };

            var selectedBooks = bookList.FindAll(book => (book.Published >= new DateTime(1994, 01, 01)));
            Console.WriteLine("Books of 1994 or later: ");
            foreach (var aBook in selectedBooks)
            {
                Console.WriteLine(aBook.Author+", "+aBook.Name+", "+Enum.GetName(typeof(Months),aBook.Published.Month)+"-"+aBook.Published.Year);
            }

            Console.ReadKey();
        }

        private enum Months
        {
            January=1,
            February,
            March,
            April,
            May,
            June,
            July,
            August,
            September,
            October,
            November,
            December
        };
    }
}

以上是关于csharp Lambda Expression(Basic ++)。cs的主要内容,如果未能解决你的问题,请参考以下文章

java Lambda expression

Expression.Call 在简单的 lambda 表达式中。可能吗?

Expression.Compile 与 Lambda、直接与虚拟调用的性能

使用 Linq.Expression 访问具有动态 lambda 的嵌套属性

Expression 生成 Lambda

ASP.NET 运算式树(Expression tree)的原理及使用