日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Csharp关键字----delegate(委托)

發(fā)布時(shí)間:2025/5/22 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Csharp关键字----delegate(委托) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Delegate類簡介
------------------------?????
命名空間:System
程序集:mscorlib(在 mscorlib.dll 中)

?? 委托(Delegate)類是一種數(shù)據(jù)結(jié)構(gòu),通過它可引用靜態(tài)方法或引用類實(shí)例及該類的實(shí)例方法。
以往的界面編程中我們應(yīng)該都接觸過各種類型的事件驅(qū)動(dòng)(event driven)的處理模式,
在這種模式里,我們定義相應(yīng)事件觸發(fā)的函數(shù)。
例如:
Button1 的 Click事件,我們可以編寫B(tài)utton1_Click 或 Btn1Clicked等函數(shù)來做相應(yīng)的驅(qū)動(dòng)處理。
而事件與驅(qū)動(dòng)函數(shù)的對應(yīng)關(guān)系就是通過委托(Delegate)類來關(guān)聯(lián)的。

其實(shí)委托(Delegate)類這種數(shù)據(jù)結(jié)構(gòu)有些類似于之前C/C++中的函數(shù)指針。

Delegate類一個(gè)簡單應(yīng)用
------------------------????
1.定義一個(gè)Delegate函數(shù)數(shù)據(jù)結(jié)構(gòu)
2.定義Delegate將引用的靜態(tài)方法或引用類實(shí)例及該類的實(shí)例方法
3.Delegate數(shù)據(jù)變量指向?qū)嵗椒?
4.通過Delegate數(shù)據(jù)變量執(zhí)行實(shí)例方法

A very basic example (TestClass.cs):

1: using System; 2: 3: namespace MySample 4: { 5: class TestClass 6: { 7: //1.定義一個(gè)Delegate函數(shù)數(shù)據(jù)結(jié)構(gòu) 8: public delegate void GoDelegate(); 9: 10: [STAThread] 11: static void Main(string[] args) 12: { 13: //3.Delegate數(shù)據(jù)變量指向?qū)嵗椒?/span> 14: GoDelegate goDelegate = new GoDelegate( MyDelegateFunc); 15: 16: //4.通過Delegate數(shù)據(jù)變量執(zhí)行實(shí)例方法 17: goDelegate(); 18: return; 19: } 20: //2.定義Delegate將引用的靜態(tài)方法或引用類實(shí)例及該類的實(shí)例方法 21: public static void MyDelegateFunc() 22: { 23: Console.WriteLine("delegate function..."); 24: } 25: } 26: }

編譯執(zhí)行結(jié)果:

# TestClass.exe
delegate function...

使用Delegate類和Override實(shí)現(xiàn)多態(tài)的比較
-----------------------------------------------

1.使用Delegate類的時(shí)候,下面的例子可以很清楚的說明。
? 1.1 首先定義一個(gè)動(dòng)物基類(MyAnimalDelegateClass), 基類中有顯示屬性的(ShowAnimalType)的public方法。
????? 并且在ShowAnimalType方法中調(diào)用Delegate引用的實(shí)例方法

? 1.2 定義獅子(LionDelegateClass)和馬(HorseDelegateClass)兩個(gè)子類。Delegate與各自的實(shí)例方法綁定
????? 實(shí)現(xiàn)不同的屬性顯示(ShowAnimalType)方法。

Delegate example (TestClass.cs):

1: AnonymousDelegatesusing System; 2: 3: namespace MySample 4: { 5: class TestClass 6: { 7: [STAThread] 8: static void Main(string[] args) 9: { 10: //獅子(LionDelegateClass)的屬性顯示(ShowAnimalType)方法調(diào)用 11: LionDelegateClass lionDelegate = new LionDelegateClass(); 12: lionDelegate.ShowAnimalType("MySample"); 13: 14: //馬(HorseDelegateClass)的屬性顯示(ShowAnimalType)方法調(diào)用 15: HorseDelegateClass horseDelegate = new HorseDelegateClass(); 16: horseDelegate.ShowAnimalType("MySample"); 17: } 18: } 19: 20: //動(dòng)物基類(MyAnimalDelegateClass) 21: public class MyAnimalDelegateClass 22: { 23: //Delegate數(shù)據(jù)結(jié)構(gòu)定義 24: public delegate void DelegateFunction(string strFuncName); 25: 26: private DelegateFunction m_delegateFunction = null; 27: 28: //Delegate類型的屬性 29: public DelegateFunction delegateFunction 30: { 31: get 32: { 33: return m_delegateFunction; 34: } 35: set 36: { 37: m_delegateFunction = value; 38: } 39: } 40: //屬性顯示(ShowAnimalType)方法 41: public void ShowAnimalType(string strFuncName) 42: { 43: if (delegateFunction != null) 44: { 45: object[] args = {strFuncName}; 46: //調(diào)用Delegate引用的實(shí)例方法 47: delegateFunction.DynamicInvoke(args); 48: } 49: } 50: } 51: 52: //獅子(LionDelegateClass) 53: public class LionDelegateClass:MyAnimalDelegateClass 54: { 55: public LionDelegateClass() 56: { 57: this.delegateFunction = new DelegateFunction(subFunction1); 58: } 59: 60: //獅子(LionDelegateClass)實(shí)例方法的實(shí)裝 61: private void subFunction1(string strFuncName) 62: { 63: System.Console.WriteLine( 64: string.Format("[{0}]This is a lion....", strFuncName)); 65: } 66: } 67: 68: //馬(HorseDelegateClass) 69: public class HorseDelegateClass:MyAnimalDelegateClass 70: { 71: public HorseDelegateClass() 72: { 73: this.delegateFunction = new DelegateFunction(subFunction2); 74: } 75: 76: //馬(HorseDelegateClass)實(shí)例方法的實(shí)裝 77: private void subFunction2(string strFuncName) 78: { 79: System.Console.WriteLine( 80: string.Format("[{0}]This is a horse....", strFuncName)); 81: } 82: } 83: }

編譯執(zhí)行結(jié)果:

# TestClass.exe

[MySample]This is a lion....
[MySample]This is a horse....

2.使用Override實(shí)裝的時(shí)候,參考下面的例子。
? 1.1 首先定義一個(gè)動(dòng)物基類(AbstractAnimalNoDelegateClass), 基類中有顯示屬性的(ShowAnimalType)的public方法。
????? 并且在ShowAnimalType方法中調(diào)用抽象方法(NoDelegateFunction)

? 1.2 定義獅子(LionNoDelegateClass)和馬(HorseNoDelegateClass)兩個(gè)子類。
????? 子類中實(shí)裝抽象方法(NoDelegateFunction)
????? 實(shí)現(xiàn)不同的屬性顯示(ShowAnimalType)方法。

Override example (TestClass.cs):

1: using System; 2: 3: namespace MySample 4: { 5: class TestClass 6: { 7: [STAThread] 8: static void Main(string[] args) 9: { 10: //獅子(LionNoDelegateClass )的屬性顯示(ShowAnimalType)方法調(diào)用 11: LionNoDelegateClass lionNoDelegate = new LionNoDelegateClass(); 12: lionNoDelegate.ShowAnimalType("MySample"); 13: 14: //馬(HorseNoDelegateClass )的屬性顯示(ShowAnimalType)方法調(diào)用 15: HorseNoDelegateClass horseNoDelegate = new HorseNoDelegateClass(); 16: horseNoDelegate.ShowAnimalType("MySample"); 17: } 18: } 19: 20: //動(dòng)物基類(AbstractAnimalNoDelegateClass) 21: public abstract class AbstractAnimalNoDelegateClass 22: { 23: public void ShowAnimalType(string strFuncName) 24: { 25: //抽象方法(NoDelegateFunction)調(diào)用 26: NoDelegateFunction(strFuncName); 27: } 28: //在基類中定義抽象方法(NoDelegateFunction) 29: protected abstract void NoDelegateFunction(string strFuncName); 30: } 31: 32: //獅子(LionNoDelegateClass ) 33: public class LionNoDelegateClass:AbstractAnimalNoDelegateClass 34: { 35: // 子類中實(shí)裝抽象方法(NoDelegateFunction) 36: protected override void NoDelegateFunction(string strFuncName) 37: { 38: System.Console.WriteLine( 39: string.Format("[{0}]This is a lion....", strFuncName)); 40: } 41: } 42: //馬(HorseNoDelegateClass ) 43: public class HorseNoDelegateClass:AbstractAnimalNoDelegateClass 44: { 45: // 子類中實(shí)裝抽象方法(NoDelegateFunction) 46: protected override void NoDelegateFunction(string strFuncName) 47: { 48: System.Console.WriteLine( 49: string.Format("[{0}]This is a horse....", strFuncName)); 50: } 51: } 52: } 53:

編譯執(zhí)行結(jié)果:

# TestClass.exe

[MySample]This is a lion....
[MySample]This is a horse....

3.比較Delegate和Override實(shí)裝方式
? 可以看出Delegate實(shí)裝方式中,相當(dāng)于定義一個(gè)函數(shù)指針的成員變量。
? 通過把實(shí)裝函數(shù)的地址賦給該成員變量,實(shí)現(xiàn)同樣的方法,處理方式的不同。
? 而Override方式中,則是在父類中預(yù)先定義好接口,通過實(shí)裝的不同,
? 來實(shí)現(xiàn)同樣的方法,處理方式的不同。
? Delegate實(shí)裝方式比較靈活,適合設(shè)計(jì)不是很完善的場合,便于修改。
? Override方式封裝性好,相對比較安全。

MulticastDelegate 類的應(yīng)用
---------------------------------
在C#中,委托(Delegate)類是多路委托,這就說可以同時(shí)指向多個(gè)處理函數(shù),
并且可以按照委托的先后順序,執(zhí)行相應(yīng)的函數(shù)。
如下例:

1: using System; 2: 3: namespace MySample 4: { 5: class TestClass 6: { 7: [STAThread] 8: static void Main(string[] args) 9: { 10: DogDelegateClass dogDelegate = new DogDelegateClass(); 11: dogDelegate.ShowAnimalType("MySample"); 12: 13: } 14: 15: public class MyAnimalDelegateClass 16: { 17: public delegate void DelegateFunction(string strFuncName); 18: 19: private DelegateFunction m_delegateFunction = null; 20: 21: public DelegateFunction delegateFunction 22: { 23: get 24: { 25: return m_delegateFunction; 26: } 27: set 28: { 29: m_delegateFunction = value; 30: } 31: } 32: 33: public void ShowAnimalType(string strFuncName) 34: { 35: if (delegateFunction != null) 36: { 37: object[] args = {strFuncName}; 38: 39: delegateFunction.DynamicInvoke(args); 40: } 41: } 42: } 43: 44: public class DogDelegateClass:MyAnimalDelegateClass 45: { 46: public DogDelegateClass() 47: { 48: //多路委托函數(shù) 設(shè)定 49: this.delegateFunction = new DelegateFunction(subFunction31); 50: this.delegateFunction += new DelegateFunction(subFunction32); 51: } 52: //委托函數(shù)1 53: private void subFunction31(string strFuncName) 54: { 55: System.Console.WriteLine( 56: string.Format("[{0}]This is a dog....", strFuncName)); 57: } 58: //委托函數(shù)2 59: private void subFunction32(string strFuncName) 60: { 61: System.Console.WriteLine( 62: string.Format("[{0}]This is a nice dog....", strFuncName)); 63: } 64: }

65: }

編譯執(zhí)行結(jié)果: # TestClass.exe [MySample]

This is a dog....[MySample]This is a nice dog....

本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/xinsir/archive/2006/08/22/1106277.aspx

1: // Copyright ? Microsoft Corporation. All Rights Reserved. 2: // This code released under the terms of the 3: // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) 4: // 5: //Copyright (C) Microsoft Corporation. All rights reserved. 6: 7: using System; 8: using System.Collections.Generic; 9: using System.Text; 10: 11: namespace AnonymousDelegate_Sample 12: { 13: 14: // Define the delegate method. 15: delegate decimal CalculateBonus(decimal sales); 16: 17: // Define an Employee type. 18: class Employee 19: { 20: public string name; 21: public decimal sales; 22: public decimal bonus; 23: public CalculateBonus calculation_algorithm; 24: } 25: 26: class Program 27: { 28: 29: // This class will define two delegates that perform a calculation. 30: // The first will be a named method, the second an anonymous delegate. 31: 32: // This is the named method. 33: // It defines one possible implementation of the Bonus Calculation algorithm. 34: 35: static decimal CalculateStandardBonus(decimal sales) 36: { 37: return sales / 10; 38: } 39: 40: static void Main(string[] args) 41: { 42: 43: // A value used in the calculation of the bonus. 44: // Note: This local variable will become a "captured outer variable". 45: decimal multiplier = 2; 46: 47: // This delegate is defined as a named method. 48: CalculateBonus standard_bonus = new CalculateBonus(CalculateStandardBonus); 49: 50: // This delegate is anonymous - there is no named method. 51: // It defines an alternative bonus calculation algorithm. 52: CalculateBonus enhanced_bonus = delegate(decimal sales) 53: { return multiplier * sales / 10; }; 54: 55: // Declare some Employee objects. 56: Employee[] staff = new Employee[5]; 57: 58: // Populate the array of Employees. 59: for (int i = 0; i < 5; i++) 60: staff[i] = new Employee(); 61: 62: // Assign initial values to Employees. 63: staff[0].name = "Mr Apple"; 64: staff[0].sales = 100; 65: staff[0].calculation_algorithm = standard_bonus; 66: 67: staff[1].name = "Ms Banana"; 68: staff[1].sales = 200; 69: staff[1].calculation_algorithm = standard_bonus; 70: 71: staff[2].name = "Mr Cherry"; 72: staff[2].sales = 300; 73: staff[2].calculation_algorithm = standard_bonus; 74: 75: staff[3].name = "Mr Date"; 76: staff[3].sales = 100; 77: staff[3].calculation_algorithm = enhanced_bonus; 78: 79: staff[4].name = "Ms Elderberry"; 80: staff[4].sales = 250; 81: staff[4].calculation_algorithm = enhanced_bonus; 82: 83: // Calculate bonus for all Employees 84: foreach (Employee person in staff) 85: PerformBonusCalculation(person); 86: 87: // Display the details of all Employees 88: foreach (Employee person in staff) 89: DisplayPersonDetails(person); 90: 91: 92: } 93: 94: public static void PerformBonusCalculation(Employee person) 95: { 96: 97: // This method uses the delegate stored in the person object 98: // to perform the calculation. 99: // Note: This method knows about the multiplier local variable, even though 100: // that variable is outside the scope of this method. 101: // The multipler varaible is a "captured outer variable". 102: person.bonus = person.calculation_algorithm(person.sales); 103: } 104: 105: public static void DisplayPersonDetails(Employee person) 106: { 107: Console.WriteLine(person.name); 108: Console.WriteLine(person.bonus); 109: Console.WriteLine("---------------"); 110: Console.ReadLine(); 111: } 112: } 113: }

轉(zhuǎn)載于:https://www.cnblogs.com/xiuyusoft/archive/2011/06/14/2080196.html

總結(jié)

以上是生活随笔為你收集整理的Csharp关键字----delegate(委托)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。