namespace 阐述lambda
{
public class Person
{
public string Name { get; set; }
public int Age { get;set; }
}
class Program
{
public static List<Person> PersonsList()
{
List<Person> persons = new List<Person>();
for (int i = 0; i < 7; i++)
{
Person p = new Person() { Name = i + "儿子", Age = 8 - i, };
persons.Add(p);
}
return persons;
}
static void Main(string[] args)
{
List<Person> persons = PersonsList();
persons = persons.Where(p => p.Age > 6).ToList(); //所有Age>6的Person的集合
Person per = persons.SingleOrDefault(p => p.Age == 1); //Age=1的单个people类
persons = persons.Where(p => p.Name.Contains("儿子")).ToList(); //所有Name包含儿子的Person的集合
}
}
}
//委托 逛超市
delegate int GuangChaoshi(int a);
static void Main(string[] args)
{
GuangChaoshi gwl = JieZhang;
Console.WriteLine(gwl(10) + ""); //打印20,委托的应用
Console.ReadKey();
}
//结账
public static int JieZhang(int a)
{
return a + 10;
}
//委托 逛超市
delegate int GuangChaoshi(int a);
static void Main(string[] args)
{
// GuangChaoshi gwl = JieZhang;
GuangChaoshi gwl = p => p + 10;
Console.WriteLine(gwl(10) + ""); //打印20,表达式的应用
Console.ReadKey();
}
//委托 逛超市
delegate int GuangChaoshi(int a,int b);
static void Main(string[] args)
{
GuangChaoshi gwl = (p,z) => z-(p + 10);
Console.WriteLine(gwl(10,100) + ""); //打印80,z对应参数b,p对应参数a
Console.ReadKey();
}
/// <summary>
/// 委托 逛超市
/// </summary>
/// <param name="a">花费</param>
/// <param name="b">付钱</param>
/// <returns>找零</returns>
delegate int GuangChaoshi(int a,int b);
static void Main(string[] args)
{
GuangChaoshi gwl = (p, z) =>
{
int zuidixiaofei = 10;
if (p < zuidixiaofei)
{
return 100;
}
else
{
return z - p - 10;
}
};
Console.WriteLine(gwl(10,100) + ""); //打印80,z对应参数b,p对应参数a
Console.ReadKey();
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |