簡單的Delegate 委託 例子

2021-04-13 11:46:18 字數 623 閱讀 9662

今天學習一下好久都不願意碰的delegate,雖然不是很明白,還是寫了乙個小例子(參考園子裡的朋友們的),希望加深一下印象.朋友們說它類似於c++的函式指標,指向乙個函式,執行的時候可以呼叫這個函式.

步驟:1. 宣告乙個delegate物件。

2. 實現和delegate具有相同引數和返回值的函式實現(可以是靜態和非靜態的)。

3. 產生乙個delegate物件的時候,把你剛剛實現的函式作為引數傳給他的建構函式。

**如下:

default.aspx.cs中:

public partial class _default : system.web.ui.page

//第二步 實現和delegate具有相同引數和返回值的函式實現(非靜態的)

//這裡的引數是mydelegate,返回值是void

public void hello(string mydelegate)

//第二步 實現和delegate具有相同引數和返回值的函式實現(靜態的)

//這裡的引數是mydelegate,返回值是void

public static void statichello(string mydelegate)

}輸出結果:hellobeijing

c 委託delegate簡單例子

code using system using system.collections.generic using system.linq using system.text namespace delegate 可以供繼承自heater的類重寫,以便繼承類拒絕其他物件對它的監視 protected ...

簡單委託例子

例子1 using system using system.collections.generic using system.text 例子2 using system using system.collections.generic using system.text namespace 介面 c...

初始delegate委託

剛剛接觸c 不足三個月,最近網上查了一下c 的delegate委託。發現delegate更類似於之前學習c語言時候的指標,可以呼叫其它方法。使用委託可以將方法作為引數來賦值和傳遞。1 定義委託 2public delegate void mytestdelegate int i 3 class pr...