次の方法で共有


方法 : デリゲートを宣言し、インスタンス化して使用する (C# プログラミング ガイド)

C# 1.0 以降では、次の例に示すようにデリゲートを宣言できます。

// Declare a delegate.
delegate void Del(string str);

// Declare a method with the same signature as the delegate.
static void Notify(string name)
{
    Console.WriteLine("Notification received for: {0}", name);
}
// Create an instance of the delegate.
Del del1 = new Del(Notify);

C# 2.0 では、次の例に示すように、前の宣言をさらに簡潔に記述できます。

// C# 2.0 provides a simpler way to declare an instance of Del.
Del del2 = Notify;

C# 2.0 以降では、次の例に示すように、匿名メソッドを使用してデリゲートを宣言および初期化することもできます。

// Instantiate Del by using an anonymous method.
Del del3 = delegate(string name)
    { Console.WriteLine("Notification received for: {0}", name); };

C# 3.0 以降では、次の例に示すように、ラムダ式を使用してデリゲートを宣言およびインスタンス化することもできます。

// Instantiate Del by using a lambda expression.
Del del4 = name =>  { Console.WriteLine("Notification received for: {0}", name); };

詳細については、「ラムダ式 (C# プログラミング ガイド)」を参照してください。

この例では、デリゲートの宣言方法、インスタンス化方法、および使用方法を示します。BookDB クラスは、書籍のデータベースを管理する書店データベースをカプセル化します。このクラスは、ProcessPaperbackBooks メソッドを公開します。このメソッドは、データベースからすべてのペーパーバックを検索し、それぞれについてデリゲートを呼び出します。使用される delegate 型の名前は、ProcessBookDelegate です。Test クラスはこのクラスを使用して、ペーパーバックの書名と平均価格を出力します。

デリゲートを使用すると、書店データベースとクライアント コードの機能の分担を適切に行うことができます。クライアント コードは、書籍の在庫状況や書店コードがペーパーバックを検索する方法については関知しません。書店コードは、ペーパーバックの検索後の処理については関知しません。

使用例

// A set of classes for handling a bookstore:
namespace Bookstore
{
    using System.Collections;

    // Describes a book in the book list:
    public struct Book
    {
        public string Title;        // Title of the book.
        public string Author;       // Author of the book.
        public decimal Price;       // Price of the book.
        public bool Paperback;      // Is it paperback?

        public Book(string title, string author, decimal price, bool paperBack)
        {
            Title = title;
            Author = author;
            Price = price;
            Paperback = paperBack;
        }
    }

    // Declare a delegate type for processing a book:
    public delegate void ProcessBookDelegate(Book book);

    // Maintains a book database.
    public class BookDB
    {
        // List of all books in the database:
        ArrayList list = new ArrayList();

        // Add a book to the database:
        public void AddBook(string title, string author, decimal price, bool paperBack)
        {
            list.Add(new Book(title, author, price, paperBack));
        }

        // Call a passed-in delegate on each paperback book to process it: 
        public void ProcessPaperbackBooks(ProcessBookDelegate processBook)
        {
            foreach (Book b in list)
            {
                if (b.Paperback)
                    // Calling the delegate:
                    processBook(b);
            }
        }
    }
}


// Using the Bookstore classes:
namespace BookTestClient
{
    using Bookstore;

    // Class to total and average prices of books:
    class PriceTotaller
    {
        int countBooks = 0;
        decimal priceBooks = 0.0m;

        internal void AddBookToTotal(Book book)
        {
            countBooks += 1;
            priceBooks += book.Price;
        }

        internal decimal AveragePrice()
        {
            return priceBooks / countBooks;
        }
    }

    // Class to test the book database:
    class TestBookDB
    {
        // Print the title of the book.
        static void PrintTitle(Book b)
        {
            System.Console.WriteLine("   {0}", b.Title);
        }

        // Execution starts here.
        static void Main()
        {
            BookDB bookDB = new BookDB();

            // Initialize the database with some books:
            AddBooks(bookDB);

            // Print all the titles of paperbacks:
            System.Console.WriteLine("Paperback Book Titles:");

            // Create a new delegate object associated with the static 
            // method Test.PrintTitle:
            bookDB.ProcessPaperbackBooks(PrintTitle);

            // Get the average price of a paperback by using
            // a PriceTotaller object:
            PriceTotaller totaller = new PriceTotaller();

            // Create a new delegate object associated with the nonstatic 
            // method AddBookToTotal on the object totaller:
            bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);

            System.Console.WriteLine("Average Paperback Book Price: ${0:#.##}",
                    totaller.AveragePrice());
        }

        // Initialize the book database with some test books:
        static void AddBooks(BookDB bookDB)
        {
            bookDB.AddBook("The C Programming Language", "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true);
            bookDB.AddBook("The Unicode Standard 2.0", "The Unicode Consortium", 39.95m, true);
            bookDB.AddBook("The MS-DOS Encyclopedia", "Ray Duncan", 129.95m, false);
            bookDB.AddBook("Dogbert's Clues for the Clueless", "Scott Adams", 12.00m, true);
        }
    }
}
/* Output:
Paperback Book Titles:
   The C Programming Language
   The Unicode Standard 2.0
   Dogbert's Clues for the Clueless
Average Paperback Book Price: $23.97
*/

信頼性の高いプログラミング

  • デリゲートの宣言

    次のステートメントは、新しいデリゲート型を宣言します。

    public delegate void ProcessBookDelegate(Book book);
    

    各デリゲート型は、引数の数、引数型、およびカプセル化できるメソッドの戻り値の型を表します。引数型または戻り値の型の新しいセットが必要になった場合には、新しいデリゲート型を宣言する必要があります。

  • デリゲートのインスタンス化

    デリゲート型を宣言したら、デリゲート オブジェクトを作成して、特定のメソッドに関連付ける必要があります。前の例では、次の例のように、PrintTitle メソッドを ProcessPaperbackBooks メソッドに渡すことにより行います。

    bookDB.ProcessPaperbackBooks(PrintTitle);
    

    このコードは、静的メソッド Test.PrintTitle と関連付けられている新しいデリゲート オブジェクトを作成します。同様に、totaller オブジェクトの静的でないメソッド AddBookToTotal は、次の例のように渡されます。

    bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);
    

    どちらの場合も、新しいデリゲート オブジェクトは ProcessPaperbackBooks メソッドに渡されます。

    デリゲートが作成されると、デリゲートに関連付けられたメソッドは変更できません。つまり、デリゲート オブジェクトは不変であることに注意してください。

  • デリゲートの呼び出し

    作成されたデリゲート オブジェクトは、通常、デリゲートを呼び出す他のコードに渡されます。デリゲート オブジェクトを呼び出すには、デリゲート オブジェクトの名前を使用します。名前の後ろには、デリゲートへ渡す引数をかっこで囲んで指定します。デリゲートの呼び出しの例を、次に示します。

    processBook(b);
    

    この例のように、デリゲートは、同期的に呼び出すことも、BeginInvoke メソッドと EndInvoke メソッドを使用して非同期的に呼び出すこともできます。

参照

関連項目

イベント (C# プログラミング ガイド)

デリゲート (C# プログラミング ガイド)

概念

C# プログラミング ガイド