• C#细节之lambda,linq,匿名方法


    看代码,得永生
    不再解释,很容易的,我没有仔细研究过,在此记下,以后深入研究。
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using System.Text;

    namespace Landpy
    {
        
    class Starter
        {
            
    delegate int Add(int i, int j);
            
    delegate bool Compare(int i, int j);
            
    delegate void NoNameMethod(string name);
            
    static void Main()
            {
                Add add 
    = (x, y) => x + y;
                Console.WriteLine(add.Invoke(
    12));
                Compare compare 
    = (i, j) => i == j;
                Console.WriteLine(compare.Invoke(
    12));
                Console.WriteLine(compare.Invoke(
    11));
                NoNameMethod noNameMethod 
    =
                    
    delegate(string name)
                    {
                        Console.WriteLine(String.Format(
    "Hello {0}!", name));
                    };
                Action
    <int> hello = delegate(int i)
                {
                    
    for (int j = 0; j < i; j++)
                    {
                        Console.WriteLine(
    "Hello");
                    }
                };
                noNameMethod.Invoke(
    "Landpy");
                hello.Invoke(
    3);

                
    string[] names = { "++landpy""pxl""ljc""baby" };
                
    //lambda的写法 
                var quereynamestwo = names.Where(n => n.Length == 3).Select(n => n.Substring(2));//.Select(n => n.Name);

                
    //linq的写法 
                var quereynames = from n in names
                                  
    where n.Length == 3
                                  select n;

                
    foreach (string name in quereynames)
                {
                    Console.WriteLine(name);
                }

                
    foreach (string name in quereynamestwo)
                {
                    Console.WriteLine(name);
                }

                ArrayList employees 
    = new ArrayList();

                
    for (int i = 0; i < 10; i++)
                {
                    Employee employee 
    = new Employee();
                    employee.Name 
    = "Name" + i.ToString();
                    employee.Age 
    = i;
                    employees.Add(employee);
                }

                Employee[] employeesArray 
    = (Employee[])employees.ToArray(typeof(Employee));

                var es 
    = employeesArray.Where(n => n.Age > 4);
                
    foreach (Employee employeeTmp in es)
                {
                    Console.WriteLine(employeeTmp.Name);
                }
            }
        }

        
    class Employee
        {
            
    public string Name
            {
                
    get;
                
    set;
            }

            
    public int Age
            {
                
    get;
                
    set;
            }
        }
    }
    敬告

    作者:pangxiaoliang
    出处:http://www.cnblogs.com/pangxiaoliang
    本文版权归pangxiaoliang和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,谢谢合作。
  • 相关阅读:
    表单
    HTML5新特性
    Bash中的特殊字符
    网站商务通链接快速标识v1.0.js
    js实现中文简繁切换效果
    input 表单点击消失离开出现
    canonical 标签介绍
    商务通对话窗口出现验证码
    织梦dedecms后台发布文章不自动更新首页与栏目列表页
    dedecms修改templets为别的名字
  • 原文地址:https://www.cnblogs.com/pangxiaoliang/p/1530654.html
Copyright © 2020-2023  润新知