引言:
最初學習c#時,感覺委托、事件這塊很難,其中在學習的過程中還寫了一篇學習筆記:委托、事件學習筆記。今天重新溫故委托、事件,並且把最近學習到和委托相關的匿名方法、Lambda表達式及泛型委托記錄下來,以備復習使用。
委托:
日常工作中,常常見到委托用在具體的項目中。而且委托使用起來相對來說也是非常簡單的,下面列舉一個委托實例用以說明如何使用委托,代碼如下:
class Program { public delegate int CalculateDelegate(int x, int y); static void Main(string[] args) { CalculateDelegate calculateDelegate = new CalculateDelegate(Add); int result = calculateDelegate(2, 3); } public static int Add(int x, int y) { return x + y; } }
從上例中可以看出,使用一個委托分為三個步驟:
1)聲明一個委托:public delegate int CalculateDelegate(int x, int y);
2)定義一個委托對象並綁定方法:CalculateDelegate calculateDelegate = new CalculateDelegate(Add);
3)調用委托:int result = calculateDelegate(2, 3);
其中第二步、第三步的寫法和大家有出入,通常很多人喜歡這樣寫:
1)聲明一個委托
2)定義一個委托要綁定的方法
3)定義一個委托,綁定上述定義的方法
匿名方法:
使用上面編寫的委托實例,描述匿名方法到底為何物,是怎么使用的。委托綁定方法實現如下:
CalculateDelegate calculateDelegate = new CalculateDelegate(Add); public static int Add(int x, int y) { return x + y; }
如果使用匿名方法重寫上面的方法,代碼如下:
CalculateDelegate calculateDelegate = delegate(int x, int y){return x + y;};
可見:匿名方法綁定委托直接省去了編寫一個單獨的方法,使得代碼更為簡潔。
項目中,使用委托時,很多時候編輯器會幫助我們把方法直接放入合適的委托對象中,但有時候編輯器幫助不了我們,比如:Control.Dispatcher.Invoke(delegate). 例如:
this.btnExit .Dispatcher .Invoke (new Action(() => {}));
注:感謝園友上位者的憐憫的意見。
Lambda表達式:
用Lambda表達式重寫上面使用匿名方法編寫的委托實例,在匿名方法的基礎上,編寫如下:
方式一:
CalculateDelegate calculateDelegate = (int x, int y) => { return x + y; };
方式二:
CalculateDelegate calculateDelegate = (x, y) => { return x + y; };
方式三:
CalculateDelegate calculateDelegate = (x, y) => x + y;
從上面可以看出,Lambda僅僅是在匿名方法的基礎上加上 => 符號,但是卻讓整個代碼實現起來顯得更為優雅。
泛型委托:
在.net平台下有Microsoft自帶的泛型委托,如:Action,Action<T>,Fun<T>等。實際使用中,如果需要用到泛型委托,系統內置的委托基本上就能滿足需求了,下面一一介紹它們的原型及調用實例。
Action
系統封裝的Action委托,沒有參數沒有返回值。調用實例為:
class Program { public delegate void Action(); static void Main(string[] args) { Action action = new Action(Method); action(); } private static void Method() { Console.WriteLine("i am is a action"); } }
如果方法的表達很簡單,可以使用Lambda表達式,代碼如下:
Action action = () => { Console.WriteLine("i am is a action"); }; action();
Action<T>
系統封裝的Action<T>委托,有參數但是沒有返回值。調用實例為:
class Program { public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2); static void Main(string[] args) { Action<int,int> action = new Action<int,int>(Method); action(2,3); } private static void Method(int x,int y) { Console.WriteLine("i am is a action"); } }
使用Lambda表達式編寫Action<T>代碼如下:
Action<int, int> action = (x, y) => { Console.WriteLine("i am is a action"); }; action(2,3);
Fun<T>
系統封裝的Fun<T>委托,有返回值。調用實例為:
class Program { public delegate TResult Fun<in T1, in T2, out TResult>(T1 arg1, T2 arg2); static void Main(string[] args) { Fun<int, int, bool> fun = new Fun<int, int, bool>(Method); bool ret = fun(2,3); } private static bool Method(int x,int y) { if (x + y == 5) { return true; } else { return false; } } }
使用Lambda表達式編寫Fun<T>,代碼如下:
Fun<int, int, bool> fun = (x, y) => { if (x + y == 5) { return true; } else { return false; } }; bool ret = fun(2,3);
Fun<T>沒有參數有返回值的情況:
Fun<bool> fun = () => { int x = 4; int y = 3; if (x + y == 5) { return true; } else { return false; } }; //也可以如此編寫 Fun<bool> fun = () => false;
此種情況很少使用,而且由於能力有限,暫且看不出來使用它們的意義所在。
總結:
在平時的項目中,常見的委托種類及實例本質上也是上述所說的幾種。除此之外還有一些其他的委托方法,資質有限精力有限,此處暫且不提,以后碰到在另當別論。