抽象メソッドはメソッド名まで定義されているが、デリゲートでは引数と戻り値だけが定義。名前は自由。というところでしょうか。使い方は下記の通りです。抽象メソッドよりも何かクールな気がしませんか?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*-----------------------------------------------------*/ | |
//引数が(int, int)で戻り値がintのメソッドを受け取るデリゲート | |
//プロパティによりメソッドを登録する | |
Func<int, int, int> method; | |
public Func<int, int, int> Method { | |
get { return method; } | |
set { method = value; } | |
} | |
private void process(){ | |
for(int i=0,i<10;i++){ | |
//iとi+1を引数として渡す(ここでは中身は決まってない) | |
System.Diagnostics.Debug.WriteLine(this.method(i,i+1)); | |
} | |
} | |
/*--------以下、処理を定義し登録する側 --------*/ | |
//xxxxは上記のクラスのインスタンス | |
//Methodプロパティで関数を登録 | |
xxxx.Method = test; | |
//登録する関数の実際の処理 | |
int test(int x,int y){ | |
return x + y; | |
} |
0 件のコメント:
コメントを投稿