初试Delegate

在.NET Framework  中包含一种称作代理的机制(delegate),它类似于C/C++的函数指针,但它是面向对象的和类型安全的。
      在c#代理同类和接口一样,也是一种引用类型,用户可以使用代理封装一个具有特定参数和返回类型的的方法,包括静态方法和实例方法,代理是实现.NET Framework中事件处理机制的基础。
          代理的声明实际上定义了一个从System.MulticastDelegate类派生的类,这个类是由C#编译器根据代理的声明语句自动生成的。
   主要有两个:
             public void virtual Invoke(object value ,int32 item,Int32 numItems);//这个方法用来调用代理实例所封装的方法
              public virtual IAsyncResult BeginInvoke(object value,int32 item,int32 numItems,AsyncCallback callback,Object object);      
            public virtual void EndInvoke(IAsyncresult result);
   ////////////////上面两个方法是用来执行异步调用
   下面看个小例字(注:本人不善意写注释
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Bookstore;

namespace Bookstore
{
    public struct Book
    {
        public string Title;
        public string Author;
        public decimal Price;
        public bool Paperback;
        //构造函数
        public Book(string title, string author, decimal price, bool paperBack)
        {
            Title = title;
            Author = author;
            Price = price;
            Paperback = paperBack;
        }

    }

    public delegate void processBookDelegate(Book book);
    /// <summary>
    ///
    /// </summary>
    public class BookDB
    {
        ArrayList list = new ArrayList();

        public void AddBook(string title, string author, decimal price, bool pagerBack)
        {
            list.Add(new Book(title, author, price, pagerBack));

        }
        public void ProcessPaperbackBooks(processBookDelegate processBook)
        {
            foreach (Book b in list)
            {
                if (b.Paperback)
                    processBook(b);
            }
        }
    }
}
namespace BookTestClient
{
    using Bookstore;
   //
    class PriceTotaller
    {
        int countBooks = 0;
        decimal priceBooks = 0.0m;
        internal void AddBookTotal(Book book)
        {
            countBooks += 1;
            priceBooks += book.Price;
        }
        internal decimal AveragePrice()
        {
            return priceBooks / countBooks;
        }
    }
    class Test
    {
        static void PrintTitle(Book b)
        {
            Console.WriteLine("{0}", b.Title);
        }

        static void Main(string[] args)
        {
            BookDB bookDB = new BookDB();

            AddBooks(bookDB);
            //
            Console.WriteLine("Paperback Book Titles:");
            bookDB.ProcessPaperbackBooks(new processBookDelegate(PrintTitle));

            //
            PriceTotaller totaller = new PriceTotaller();
            //
            bookDB.ProcessPaperbackBooks(new processBookDelegate(totaller.AddBookTotal));
            Console.WriteLine("Average Paperback Book Price :${0:#.##}", totaller.AveragePrice());
          
          
        }
        static void AddBooks(BookDB bookDB)
        {
            bookDB.AddBook("The c Programming Language", "Brian W. Kernighan and Dennis M. Ritchie", 111.95m, true);
            bookDB.AddBook("The Unicode Standard 2.0 ", "The Unicode Consortium", 311.95m, true);
            bookDB.AddBook("The ms_dos Encyclopedia", "Ray Duncan", 1211.95m,false);
            bookDB.AddBook("Dogbert 's Clues for the clueless", "Scott Adams", 12.00m, true);
        }
    }
   
}

 

posted @ 2007-03-13 11:17  上海-天浩  阅读(196)  评论(0编辑  收藏  举报

Living in ShangHai

Copyright © 2008 天浩阁 Corporation, All Rights Reserved