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

推荐订阅源

V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
V
V2EX
B
Blog RSS Feed
有赞技术团队
有赞技术团队
博客园 - Franky
美团技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
云风的 BLOG
云风的 BLOG
L
LangChain Blog
GbyAI
GbyAI
The Cloudflare Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
博客园 - 【当耐特】
The Register - Security
The Register - Security
大猫的无限游戏
大猫的无限游戏
D
Docker
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
博客园_首页
A
About on SuperTechFans
J
Java Code Geeks
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
G
Google Developers Blog
小众软件
小众软件
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
F
Full Disclosure
Jina AI
Jina AI
H
Help Net Security

博客园 - 东国先生

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
*/