2010年1月7日木曜日

delegateキーワードを使用してDelegateを定義して、そのDelegateにMethodを登録してDelegateを通してMethodを実行してみる。

using System;


// Delegateを定義
delegate void SampleDelegate(int n,string s);


class MainClass
{
    // Methodを定義
    static void method(int n,string s)
    {
        // 出力値 n:s
        Console.WriteLine("{0}:{1}",n,s);
    }


    static void Main()
    {
        // SampleDelegate型の変数sdを宣言
        SampleDelegate sd;
        // 変数sdのインスタンス化
        sd = new SampleDelegate(method);
        // 出力値:1:Sample1
        sd(1,"Sample1");
        // 出力値:2:Sample2
        sd(2,"Sample2");
    }
}

0 コメント:

コメントを投稿