lambda表达式1
Lambda表达式
Sample 1
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ public delegate int mydg(int a,int b); public static class LambdaTest { public static int oper(this int a, int b, mydg dg) { return dg(a, b); } } class Program { static void Main(string[] args) { Console.WriteLine(1.oper(2, (a, b) => a + b)); Console.WriteLine(2.oper(1, (a, b) => a - b)); Console.ReadLine(); } }}31
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ public class Person { public string username { get; set; } public int age { get; set; } public override string ToString() { return string.Format("username:{0} age:{1}", this.username, this.age); } } class Program { static void Main(string[] args) { var persons = new List<Person> { new Person {username = "a", age=19}, new Person {username = "b", age=20}, new Person {username = "c", age=21}, }; var selectPerson = from p in persons where p.age >= 20 select p.username.ToUpper(); foreach (var p in selectPerson) Console.WriteLine(p); Console.ReadLine(); } }}