委託、事件--委託例項篇
例項一、
- using System;
- namespace SimpleDelegate
- {
- //定義委託
- delegate double DoubleOp(double x);
- class MainEntryPoint
- {
- static void Main()
- {
- //例項化
- DoubleOp[] perations =
- {
- new DoubleOp(MathsOperations.MultiplyByTwo),
- new DoubleOp(MathsOperations.Square)
- };
- /* 使用匿名方法
- DoubleOp first = delegate(double value){return value * 2;};
- DoubleOp second = delegate(double value) { return value * value; };
- DoubleOp[] perations = { first, second };
- */
- //使用委託
- for (int i = 0; i < operations.Length; i++)
- {
- Console.WriteLine("Using operations[{0}]:", i);
- ProcessAndDisplayNumber(operations[i], 2.0);
- ProcessAndDisplayNumber(operations[i], 7.94);
- ProcessAndDisplayNumber(operations[i], 1.414);
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- static void ProcessAndDisplayNumber(DoubleOp action, double value)
- {
- double result = action(value);
- Console.WriteLine("Value is {0}, result of operation is {1}", value, result);
- }
- }
- class MathsOperations
- {
- public static double MultiplyByTwo(double value)
- {
- return value * 2;
- }
- public static double Square(double value)
- {
- return value * value;
- }
- }
- }
這段程式碼的關鍵一行是把委託傳遞給ProcessAndDisplayNumber()方法,例如:ProcessAndDisplayNumber(operations[i], 2.0);
其中傳遞了委託名,但不帶任何引數,假定operations[i]是一個委託,其語法是:
1、operations[i]表示“這個委託”。換言之,就是委託代表的方法。
2、operations[i](2.0)表示“呼叫這個方法,引數放在括號中”。
例項二、
- using System;
- namespace BubbleSorter
- {
- //定義委託
- delegate bool CompareOp(object lhs, object rhs);
- class MainEntryPoint
- {
- static void Main()
- {
- Employee[] employees =
- {
- new Employee("Bugs Bunny", 20000),
- new Employee("Elmer Fudd", 10000),
- new Employee("Daffy Duck", 25000),
- new Employee("Wiley Coyote", (decimal)1000000.38),
- new Employee("Foghorn Leghorn", 23000),
- new Employee("RoadRunner'", 50000)};
- //例項化
- CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater);
- BubbleSorter.Sort(employees, employeeCompareOp);
- for (int i = 0; i < employees.Length; i++)
- Console.WriteLine(employees[i].ToString());
- Console.ReadLine();
- }
- }
- class Employee
- {
- private string name;
- private decimal salary;
- public Employee(string name, decimal salary)
- {
- this.name = name;
- this.salary = salary;
- }
- public override string ToString()
- {
- return string.Format(name + ", {0:C}", salary);
- }
- //委託呼叫的方法
- public static bool RhsIsGreater(object lhs, object rhs)
- {
- Employee empLhs = (Employee)lhs;
- Employee empRhs = (Employee)rhs;
- return (empRhs.salary > empLhs.salary) ? true : false;
- }
- }
- class BubbleSorter
- {
- static public void Sort(object[] sortArray, CompareOp gtMethod)
- {
- for (int i = 0; i < sortArray.Length; i++)
- {
- for (int j = i + 1; j < sortArray.Length; j++)
- {
- //使用委託中的方法比較
- if (gtMethod(sortArray[j], sortArray[i]))
- {
- object temp = sortArray[i];
- sortArray[i] = sortArray[j];
- sortArray[j] = temp;
- }
- }
- }
- }
- }
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-608416/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- javascript事件委託程式碼例項JavaScript事件
- 事件委託事件
- 委託與事件-委託詳解(一)事件
- jQuery 事件委託jQuery事件
- javascript委託程式碼例項JavaScript
- 委託、Lambda表示式、事件系列07,使用EventHandler委託事件
- 委託、Lambda表示式、事件系列04,委託鏈是怎樣形成的, 多播委託, 呼叫委託鏈方法,委託鏈異常處理事件
- 委託、Lambda表示式、事件系列01,委託是什麼,委託的基本用法,委託的Method和Target屬性事件
- C# 事件委託C#事件
- C#事件委託事件
- 重中之重:委託與事件事件
- C# 委託事件C#事件
- 事件模型和事件委託事件模型
- C#自學(一)委託(delegate)、委託泛型、多播委託C#泛型
- jQuery程式碼優化:事件委託篇jQuery優化事件
- 委託、Lambda表示式、事件系列05,Action委託與閉包事件
- Js 事件原理與事件委託JS事件
- JS事件流和事件委託JS事件
- JQuery7:事件委託jQuery事件
- C#委託與事件C#事件
- JavaScript 事件委託詳解JavaScript事件
- C# 事件 vs 委託C#事件
- 委託、Lambda表示式、事件系列03,從委託到Lamda表示式事件
- 委託與事件-事件詳解(二)事件
- 委託、Lambda表示式、事件系列02,什麼時候該用委託事件
- 事件的捕獲、冒泡、委託事件
- C# 之委託與事件C#事件
- javascript中的事件委託(代理)JavaScript事件
- jquery-中的事件委託jQuery事件
- Zepto事件委託的小坑事件
- js 事件的冒泡和委託JS事件
- .NET進階篇02-Delegate委託、Event事件事件
- JavaScript 事件委託或者事件代理介紹JavaScript事件
- js--事件流、事件委託、事件階段JS事件
- 事件委託詳解最新版事件
- 對js事件委託的瞭解JS事件
- jquery中如何使用事件委託?jQuery事件
- .NET委託,事件和Lambda表示式事件