简单的委托学习
来源:互联网 发布:matlab中遍历数组 编辑:程序博客网 时间:2024/05/02 12:22
委托---->匿名表达式--->lambda表达式;这是一个进化的过程。
委托:就是用委托代替一个方法,实例化为一个类,就可以作为参数来使用。可进行+、-
MSDN:
public delegate void Del(string message);// Create a method for a delegate.public static void DelegateMethod(string message){ System.Console.WriteLine(message);}Del handler = DelegateMethod;// Call the delegate.handler("Hello World");
匿名方法:在委托的基础上,不用在写命名方法。直接给委托写上方法。
MSDN:
// Create a delegate.delegatevoid Del(int x);// Instantiate the delegate using an anonymous method.Del d = delegate(int k) { /* ... */ };Lambda:用表达式的形式使用匿名方法。先声明一个委托类,创建一个委托变量,对其进行赋值。可以说是变量和函数的结合吧。MSDN:delegate int del(int i);static void Main(string[] args){ del myDelegate = x => x * x; int j = myDelegate(5); //j = 25}
//a Testdelegate bool D();delegate bool D2(int i);class Test{ D del; D2 del2; public void TestMethod(int input) { int j = 0; // Initialize the delegates with lambda expressions. // Note access to 2 outer variables. // del will be invoked within this method. del = () => { j = 10; return j > input; }; // del2 will be invoked after TestMethod goes out of scope. del2 = (x) => {return x == j; }; // Demonstrate value of j: // Output: j = 0 // The delegate has not been invoked yet. Console.WriteLine("j = {0}", j); // Invoke the delegate. bool boolResult = del(); // Output: j = 10 b = True Console.WriteLine("j = {0}. b = {1}", j, boolResult); } static void Main() { Test test = new Test(); test.TestMethod(5); // Prove that del2 still has a copy of // local variable j from TestMethod. bool result = test.del2(10); // Output: True Console.WriteLine(result); Console.ReadKey(); }}
- 学习简单的委托
- 简单的委托学习
- 委托学习笔记:最简单的委托
- c#委托的简单学习
- 委托delegate的学习.+一个简单的委托和委托链的例子
- 委托delegate的学习.+一个简单的委托和委托链的例子
- 委托的简单例子
- 委托的简单例子
- 一个简单的委托
- 简单的委托示例
- 简单的委托实例
- 简单的委托+事件
- 简单的委托
- 委托的简单应用
- 简单的委托
- 简单的委托使用
- 简单的委托
- ios自用学习笔记一委托协议的简单了解
- android 后退键
- iphone开发 地图线路
- 程序员自学之旅(五)线程
- 看似积极,其实焦虑。
- observer模式
- 简单的委托学习
- 女人,你要沉得住气
- Intent中的四个重要属性——Action、Data、Category、Extras
- 性能测试结果的分析原则:
- 3.5.8 内核映射------《深入Linux内核架构》笔记
- 腾讯QQ新浪网易搜狐微博分享文章代码
- 找朋友
- 鲍尔默:Skype将被整合进商业通讯产品Lync
- Rails 3 Rspec spork