lambda 是實例化委託的一個參數,是一個方法
1.0 委託
2.0 匿名方法,delegate 關鍵字
3.0 lambda 表達式,參數列表 => goesto 方法體
4.0 省略參數類型,參數類型由委託推算出來,是語法糖
5.0 如何方法體只有一行,能夠去掉大括號,分號
6.0 能夠省掉 new 關鍵字this
objec,var 匿名類3.0
4.0 dynamic 避開編譯器檢查
var 匿名類,是隻讀的,var是語法糖,由編譯器自動推算編譯器
public delegate void NoReturnNoParameter();
public delegate void WithReturnWithPara(int x, int y);it
public void Show()
{編譯
}
public void Show3(int id ,int name)
{
}lambda
{
//1.0 委託
NoReturnNoParameter method = new NoReturnNoParameter(this.Show);
}object
{
//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);
}model
{
//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} 高級班")
);d3
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; }