• Lambda 表达式 问题


    1、Lambda表达式可以应用于任何匿名方法可以应用的场合。

        好处在于:简化编程,更节省编码时间。

    2、Predicate<T> 类型,

       (泛型委托)

       (它用于包装)

        任何 输入参数类型为T

        且

        返回值为bool值的方法。如:

        Predicate<int> callback = new Predicate<int> (IsEvenNumber) ;

        static bool IsEvenNumber(int i)

        {

                return(i % 2) == 0;

        }  

    3、examples.

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

    namespace LambdaSample
    {
        class Program
        {
            public static void Main()
            {
                LambdaSystax();
                Console.ReadLine();
            }
            public static void LambdaSystax()
            {
                List<int> list = new List<int>();
                list.AddRange(new int[] { 20,28,4,9,38 });
                List<int> evenNumbers = list.FindAll((i) =>
                    {
                        Console.WriteLine("value of i is currently: {0}", i);
                        bool isEven = ((i % 2) == 0);
                        return isEven;
                    });
                Console.WriteLine("Here are your even numbers:");
                foreach (int evenNumber in evenNumbers)
                {
                    Console.WriteLine("{0}\t", evenNumber);
                }

            }
        }
    }
    /*
    namespace LambdaSample
    {
        class Program
        {
            public static void Main()
            {
                List<string> dinosaurs = new List<string>();

                dinosaurs.Add("Compsognathus");
                dinosaurs.Add("Amargasaurus");
                dinosaurs.Add("Oviraptor");
                dinosaurs.Add("Velociraptor");
                dinosaurs.Add("Deinonychus");
                dinosaurs.Add("Dilophosaurus");
                dinosaurs.Add("Gallimimus");
                dinosaurs.Add("Triceratops");

                Console.WriteLine();
                foreach (string dinosaur in dinosaurs)
                {
                    Console.WriteLine(dinosaur);
                }

                Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
                    dinosaurs.TrueForAll(EndsWithSaurus));

                Console.WriteLine("\nFind(EndsWithSaurus): {0}",
                    dinosaurs.Find(EndsWithSaurus));

                Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
                    dinosaurs.FindLast(EndsWithSaurus));

                Console.WriteLine("\nFindAll(EndsWithSaurus):");
                List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);

                foreach (string dinosaur in sublist)
                {
                    Console.WriteLine(dinosaur);
                }

                Console.WriteLine(
                    "\n{0} elements removed by RemoveAll(EndsWithSaurus).",
                    dinosaurs.RemoveAll(EndsWithSaurus));

                Console.WriteLine("\nList now contains:");
                foreach (string dinosaur in dinosaurs)
                {
                    Console.WriteLine(dinosaur);
                }

                Console.WriteLine("\nExists(EndsWithSaurus): {0}",
                    dinosaurs.Exists(EndsWithSaurus));
                Console.ReadLine();
            }

            // Search predicate returns true if a string ends in "saurus".
            private static bool EndsWithSaurus(String s)
            {
                if ((s.Length > 5) &&
                    (s.Substring(s.Length - 6).ToLower() == "saurus"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    */

    /*
    namespace LambdaSample
    {
        class Program
        {

            public static void Main()
            {
                // Create an array of five Point structures.
                Point[] points = { new Point(100, 200),
                new Point(200, 300), new Point(300, 400),
                new Point(275, 395), new Point(295, 450) };

                // To find the first Point structure for which X times Y
                // is greater than 100000, pass the array and a delegate
                // that represents the ProductGT10 method to the Shared
                // Find method of the Array class.
                Point first = Array.Find(points, ProductGT10);

                // Note that you do not need to create the delegate
                // explicitly, or to specify the type parameter of the
                // generic method, because the C# compiler has enough
                // context to determine that information for you.

                // Display the first structure found.
                Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
                Console.ReadLine();
            }

            // This method implements the test condition for the Find
            // method.
            private static bool ProductGT10(Point p)
            {
                if (p.X * p.Y > 100000)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }


            //static void Main(string[] args)
            //{
            //}
        }
    }*/

  • 相关阅读:
    Basic and Digest Access Authentication 及HttpClient实现
    SQL Server2000 解析XML
    ashx+jQuery,一个轻量级的asp.net ajax解决方案
    sql 获取整时 整分秒 数据
    无法使用前导.. 在顶级目录上退出
    sql查询分析器调用自定义函数
    SqlServer 重置自动编号起始值
    asp.net不使用ViewState和服务器控件
    解决 "错误7405:异类查询要求为连接设置ANSI_NULLS和ANSI_WARNINGS
    ASP.NET服务器控件几乎都有的成员
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1516538.html
Copyright © 2020-2023  润新知