2012年4月29日日曜日

開発環境

  • Microsoft Windows 7 Home Premium (OS)
  • Microsoft Visual C# 2010 Express Edition (IDE)
  • 言語: C#

独習C# 第3版 ハーバート・シルト (著) エディフィストラーニング株式会社 矢嶋聡 (監修, 翻訳) の第12章(Cデリゲート、イベント、名前空間)の理解度5チェック12、13を解いてみる。

12、13.

コード

using System;

namespace Util
{
    delegate void EventHandler(string str);
    class Sender
    {
        public event EventHandler Notify;
        public void Fire(string str)
        {
            if (Notify != null)
            {
                str += "さん、こんにちは。";
                Notify(str);
            }
        }
    }
}

namespace Prog
{
    class Receiver
    {
        static void OnNotify(string str)
        {
            Console.WriteLine(str);
        }
        static void Main()
        {
            Util.Sender sender = new Util.Sender();
            sender.Notify += OnNotify;
            sender.Notify += delegate(string str)
            {
                Console.WriteLine("called!");
            };
            sender.Fire("太郎");
        }
    }
}

入出力結果(Console Window)

太郎さん、こんにちは。
called!
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿