c#中匿名函数lamb表达式

时间:2023-12-20 17:19:20

c#中匿名函数lamb表达式

实例一:(其实,这样都是些语法糖)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
//c#中的匿名函数
//申明一委托
delegate void Del();
class Program
{ static void show()
{
Console.WriteLine("show ......");
} static void Test()
{
Del d = new Del(show);
d();
}
static void Test2()
{
//你可以这么写....
Del d = delegate()
{
Console.WriteLine("show......");
};
d(); //你也可以这么写
Del d1 = delegate
{
Console.WriteLine("show.....");
}; d1();
//你还可以这么写;这就是lamb表达式;
Del d2 = () =>
{
Console.WriteLine("show.....");
};
d2(); }
static void Main(string[] args)
{ Test2();
Console.ReadLine(); }
}
}

有参数的lamb表达式:

 static void Test()
{
//你可以这么写
Dele d = delegate(int j)
{
Console.WriteLine(j);
};
d(); Dele d1 = (j) => { Console.WriteLine(j); };
d1();
//这个就是有参参的lamb表达式;
} static void Main(string[] args)
{
Test();
Console.ReadLine(); }

顺便提一下c#中的Action Func Predicate;

Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。

Action是无返回值的泛型委托。

Action 表示无参,无返回值的委托

Action<int,string> 表示有传入参数int,string无返回值的委托

Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托

Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托

Action至少0个参数,至多16个参数,无返回值。

Func是有返回值的泛型委托

   Func<int> 表示无参,返回值为int的委托

   Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

   Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

   Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托

   Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

 (4) predicate

   predicate 是返回bool型的泛型委托

   predicate<int> 表示传入参数为int 返回bool的委托

   Predicate有且只有一个参数,返回值固定为bool

实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading; namespace ConsoleApplication2
{ delegate void Dele(int i);
class Program
{
static void add(int i,int j)
{
Console.WriteLine(i+j);
}
static int sub(int i, int j)
{
return i - j;
} static bool isTrue(int i)
{
if (i > )
return true;
else
return false;
} static void Test()
{ List<Action<int,int>> list = new List<Action<int,int>>();
list.Add(add);
//调用方式一
list[](,);
//最后一个参数是返回值;
Func<int,int,int> func=sub;
int result=func(,);
Console.WriteLine(result); Predicate<int> p = isTrue;
bool re = p();
Console.WriteLine(re); }
static void Test2()
{
//当然我们可以换一种方式写滴呀
Action<int, int> action = delegate(int i, int j)
{
Console.WriteLine(i+j);
};
action(,); Func<int, int, int> func = delegate(int i, int j)
{
return i - j;
};
int result = func(,); Predicate<int> pre = delegate(int i)
{
bool re=
i==?true: false;
return re;
}; }
static void Test3()
{
//当然我们也可以这样写滴呀
Action<int, int> action = (i, j) => { Console.WriteLine(i + j); };
action(,); Func<int,int,int> func=(i,j)=>{return i-j;};
int result=func(,); Predicate<int> pre = (i) => { if (i == )return true; else return false; };
bool val = pre(); } static void Test4()
{
Thread thread = new Thread(delegate()
{
Console.WriteLine("hahhahah....");
});
thread.Start(); Thread thread2 = new Thread(() =>
{
Console.WriteLine("hahah......");
});
thread2.Start(); } static void Main(string[] args)
{
//其实,就相当于这样滴呀: public delegate void Action<T>(T arg);
Test();
Console.ReadLine(); }
}
}