lambda 是实例化委托的一个参数,是一个方法
1.0 委托
2.0 匿名方法,delegate 关键字
3.0 lambda 表达式,参数列表 => goesto 方法体
4.0 省略参数类型,参数类型由委托推算出来,是语法糖
5.0 如何方法体只有一行,可以去掉大括号,分号
6.0 可以省掉 new 关键字
objec,var 匿名类3.0
4.0 dynamic 避开编译器检查
var 匿名类,是只读的,var是语法糖,由编译器自动推算
public delegate void NoReturnNoParameter();
public delegate void WithReturnWithPara(int x, int y);
public void Show()
{
}
public void Show3(int id ,int name)
{
}
{
//1.0 委托
NoReturnNoParameter method = new NoReturnNoParameter(this.Show);
}
{
//2.0 匿名方法,delegate 关键字;
int i = 10;
WithReturnWithPara method3 = new WithReturnWithPara(delegate (int id, int name)
{
Console.WriteLine("i="+i);
Console.WriteLine($"{id}{name} 高级班");
});
method3.Invoke(123,456);
}
{
//3.0 lambda 表达式,参数列表 => goesto 方法体
int i = 10;
WithReturnWithPara method3 = new WithReturnWithPara( (int id, int name)=>
{
Console.WriteLine("i=" + i);
Console.WriteLine($"{id}{name} 高级班");
});
method3.Invoke(123, 456);
}
{
//4.0 省略参数类型,参数类型由委托推算出来,是语法糖
int i = 10;
WithReturnWithPara method3 = new WithReturnWithPara((id,name) =>
{
Console.WriteLine("i=" + i);
Console.WriteLine($"{id}{name} 高级班");
});
method3.Invoke(123, 456);
}
{
//5.0 如何方法体只有一行,可以去掉大括号,分号
int i = 10;
WithReturnWithPara method3 = new WithReturnWithPara((id, name)
=>Console.WriteLine($"{id}{name} 高级班")
);
method3.Invoke(123, 456);
}
{
//6.0 可以省掉 new 关键字;
int i = 10;
WithReturnWithPara method3 = (id, name)=> Console.WriteLine($"{id}{name} 高级班");
method3.Invoke(123, 456);
}
{
WithReturnWithPara method = new WithReturnWithPara(Show3);
method += this.Show3;
method += (id, name) => Console.WriteLine();
}
{
//objec,var 匿名类3.0
object model = new
{
id = 2,
name = "csj",
age = 22
};
// model.id; 不能点出来字段,编译器不认可,只能传obj,运行时认可;
}
{
//4.0 dynamic 避开编译器检查
dynamic dmodel = new
{
Id=1,
name="csj"
};
Console.WriteLine(dmodel.Id);
}
{
//var 匿名类,是只读的,var是语法糖,由编译器自动推算
var vmodel = new
{
id = 3,
name = "csj"
};
Console.WriteLine(vmodel.id);
Console.WriteLine(vmodel.GetType());
//vmodel.id = 2;
}