デリゲートの説明としてよく
処理の委譲とか関数ポインタとかでてきますが、
cはさわりしか知らないので、なかなか理解できませんでした。
結局のところ
デリゲートを使用すると
関数を引数に利用できる!
という点に尽きるのではないでしょうか。
下記はデリゲートの使用例です。
ただし、このサンプルではデリゲートを利用する意味はありませんのでご注意ください・・・
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
private delegate String DelegateFunc(String msg);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DelegateFunc df = new DelegateFunc(DelegateMethod);
Console.WriteLine(df("hello"));
Console.WriteLine(TestMethod(df));
}
private String DelegateMethod(String msg)
{
return DateTime.Now.ToString("HH:mm:ss") + " " + msg;
}
private String TestMethod(DelegateFunc df)
{
return df("good day");
}
}
}
