C 「委託」學習筆記

2021-06-12 00:02:17 字數 994 閱讀 2930

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace delegatetest

; console.writeline(sh3("jiang"));

//組合委託,此時委託就能夠依次執行多個方法

numberhandle nh = new numberhandle(c.doublenumber);

numberhandle nh2 = c.treblenumber;

numberhandle nh3 = nh + nh2;

int num = 2;

nh3(ref num);

console.writeline(""+num);

//組合委託,若委託的方法有返回值,則返回值為最後乙個被執行方法的返回值

sayhello sh4 = sh + sh3;

console.writeline(sh4("zheng"));

//lambda表示式:另一種把委託與匿名方法關聯起來的方法

sayhello sh5 = var => var + ",hello lambda";

console.writeline(sh5("guo"));

console.readline();}}

delegate string sayhello(string val);

delegate void numberhandle(ref int num);

class class1

public void doublenumber(ref int num)

public void treblenumber(ref int num)

}}

C 學習筆記 委託

什麼是委託?委託 delegate 是一種可以把引用儲存為函式的型別。委託的宣告非常類似於函式,但不帶函式體 且要使用 delegate關鍵字。委託的宣告指定了乙個型別和引數列表。在定義了乙個委託後就可以宣告該委託型別的變數。接著把這個變數初始化為與委託有相同返回型別和引數列表的函式引用。之後,就可...

C 委託學習筆記

namespace delegate private static void chinesegreeting string name 注意此方法,它接受乙個greetingdelegate型別的方法作為引數 private static void greetpeople string name,gr...

C 學習筆記 委託

學習目的 了解委託的概念 using system using system.collections.generic using system.linq using system.text using system.threading.tasks namespace example3 13 定義乙個...