惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
The GitHub Blog
The GitHub Blog
L
LangChain Blog
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
V
V2EX
MyScale Blog
MyScale Blog

博客园 - 东国先生

MongoDB - 安装及相关资料 8种Nosql数据库系统对比 Apache 服务器安装和配置相关资料 ffmpeg: error while loading shared libraries: libavdevice.so.53 亚马逊S3 - The difference between the request time and the current time is too large. Imagick setFont() 不能设置字体问题 当Linux中glibc被无意删除后…… vim终端下中文乱码问题 解决Linux中文乱码 Linux下PHP文件操作提示无权限 还原sql server数据库时,无法获得对数据库的独占访问权 zen-cart开发教程 - 通知者/观察者模式 zen-cart开发教程 - 开发Sidebox插件 修改zen-cart下单和付款流程以防止漏单 利用反射设置对象的属性(Property) 【转载】Paypal 标准变量列表 zen-cart开发教程 - 概述 PHP文件操作函数 2009年终总结
C#中的委托
东国先生 · 2010-03-02 · via 博客园 - 东国先生

// 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.95mtrue);
            bookDB.AddBook(
"The Unicode Standard 2.0""The Unicode Consortium"39.95mtrue);
            bookDB.AddBook(
"The MS-DOS Encyclopedia""Ray Duncan"129.95mfalse);
            bookDB.AddBook(
"Dogbert's Clues for the Clueless""Scott Adams"12.00mtrue);
        }
    }
}
/* 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
*/