1、委託調用方式javascript
delegate string PlusStringHandle(string x, string y); class Program { static void Main(string[] args) { PlusStringHandle pHandle = new PlusStringHandle(plusString); Console.WriteLine(pHandle("abc", "edf")); Console.Read(); } static string plusString(string x, string y) { return x + y; } }
PlusStringHandle pHandle = new PlusStringHandle(delegate(string x, string y) { return x + y; }); Console.WriteLine(pHandle("abc", "edf"));
PlusStringHandle pHandle = (string x, string y) => { return x + y; }; Console.WriteLine(pHandle("abc", "edf"));
還有更甚的寫法(省去參數類型)html
PlusStringHandle pHandle = (x, y) => { return x + y; }; Console.WriteLine(pHandle("abc", "edf"));
若是隻有一個參數java
delegate void WriteStringHandle(string str); static void Main(string[] args) { //若是隻有一個參數 WriteStringHandle handle = p => Console.WriteLine(p); handle("lisi"); Console.Read(); }
2、委託聲明方式編程
static void Main(string[] args) { WritePrint<int>(p => Console.WriteLine("{0}是一個整數", p), 10); Console.Read(); } static void WritePrint<T>(Action<T> action, T t) { Console.WriteLine("類型爲:{0},值爲:{1}", t.GetType(), t); action(t); }
class Program { static void Main(string[] args) { string str = "全部童鞋:".plusString(p => p = p + " girl: lisi、lili\r\n").plusString(p => p + "boy: wangwu") ; Console.WriteLine(str); Console.Read(); } } static class Extentions { public static string plusString<TParam>(this TParam source, Func<TParam, string> func) { Console.WriteLine("字符串相加前原值爲:{0}。。。。。。", source); return func(source); } }
看這個代碼是否是和咱們平時寫的"list.Where(p => p.Age > 18)"很像呢?沒錯Where等方法就是使用相似的方式來實現的。函數
好了,我總結完了,若有遺漏,還望補上,臣不盡感激。post
出處:http://www.cnblogs.com/FreeDong/archive/2013/07/31/3227638.htmlthis