c 重要特性之一委託

2022-02-22 13:08:25 字數 1088 閱讀 7636

委託的構成必須滿足的4個條件:

宣告委託型別;

必須有乙個方法包含了要執行的**;

必須建立乙個委託例項;

必須呼叫(invoke)委託例項

委託包裝的方法需要滿足以下條件

示例一:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

//該方法的定義必須與委託定義相同,即返回型別void,兩個int型別的引數

void add(int parm1,int parm2)

//方法的引數是委託型別

private static void mymethod(mydelegate mydelegate)

}}

示例二:

using

system;

using

system.collections.generic;

using

system.linq;

using

system.text;

using

system.threading.tasks;

namespace

//定義委託型別

public

delegate

void greetingdelegate(string

name);

//有了委託之後可以像下面這樣實現打招呼方法

public

void greeting(string

name,greetingdelegate callback)

//美國人打招呼方法

public

void englishgreeting(string

name)

//中國人打招呼方法

public

void chinesegreeting(string

name)

}}

總結:

C 特性(Attribute) 之一

c 特性 attribute 之一 by sadaf alvi 翻譯 cppbug itwizard 163.com 緒論 特性 attributes 是一種嶄新的宣告性資訊。我們不僅可以通過特性來定義設計層面的資訊 例如help file,url for documentation 以及執行時 r...

C 的重要特性 模板

template 雖然很重要,但它與 運用mfc 有什麼關係?有!第 章當我們開始設計scribble 程式時,需要用到mfc 的collection classes,而這一組類別自從mfc 3.0以來就有了template 版本 因為visual c 編譯器從2.0 版開始支援c template...

C 11新特性之一 auto

c 是一門偉大的語言,永遠給程式設計師最大的設計自由,未使用的特性從不產生 新版本永遠完全相容舊版本。c 11先前被稱作c 0x,即iso iec 14882 2011,是c 程式語言的乙個標準。c 98 auto 早在c 98標準中就存在了auto關鍵字,那時的auto用於宣告變數為自動變數,自動...