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(12); Dele d1 = (j) => { Console.WriteLine(j); }; d1(12); //這個就是有參參的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 > 0) return true; else return false; } static void Test() { List<Action<int,int>> list = new List<Action<int,int>>(); list.Add(add); //調用方式一 list[0](100,10); //最后一個參數是返回值; Func<int,int,int> func=sub; int result=func(100,10); Console.WriteLine(result); Predicate<int> p = isTrue; bool re = p(100); Console.WriteLine(re); } static void Test2() { //當然我們可以換一種方式寫滴呀 Action<int, int> action = delegate(int i, int j) { Console.WriteLine(i+j); }; action(100,10); Func<int, int, int> func = delegate(int i, int j) { return i - j; }; int result = func(100,10); Predicate<int> pre = delegate(int i) { bool re= i==0?true: false; return re; }; } static void Test3() { //當然我們也可以這樣寫滴呀 Action<int, int> action = (i, j) => { Console.WriteLine(i + j); }; action(100,10); Func<int,int,int> func=(i,j)=>{return i-j;}; int result=func(100,10); Predicate<int> pre = (i) => { if (i == 0)return true; else return false; }; bool val = pre(100); } 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(); } } }