舉例講解C#編程中委托的實(shí)例化使用
合并委托
本示例演示如何創(chuàng)建多播委托。 委托對(duì)象的一個(gè)有用屬性是:可以使用 + 運(yùn)算符將多個(gè)對(duì)象分配給一個(gè)委托實(shí)例。多播委托包含已分配委托的列表。在調(diào)用多播委托時(shí),它會(huì)按順序調(diào)用列表中的委托。只能合并相同類型的委托。
- 運(yùn)算符可用于從多播委托中移除組件委托。
using System;
// Define a custom delegate that has a string parameter and returns void.
delegate void CustomDel(string s);
class TestClass
{
// Define two methods that have the same signature as CustomDel.
static void Hello(string s)
{
System.Console.WriteLine(" Hello, {0}!", s);
}
static void Goodbye(string s)
{
System.Console.WriteLine(" Goodbye, {0}!", s);
}
static void Main()
{
// Declare instances of the custom delegate.
CustomDel hiDel, byeDel, multiDel, multiMinusHiDel;
// In this example, you can omit the custom delegate if you
// want to and use Action<string> instead.
//Action<string> hiDel, byeDel, multiDel, multiMinusHiDel;
// Create the delegate object hiDel that references the
// method Hello.
hiDel = Hello;
// Create the delegate object byeDel that references the
// method Goodbye.
byeDel = Goodbye;
// The two delegates, hiDel and byeDel, are combined to
// form multiDel.
multiDel = hiDel + byeDel;
// Remove hiDel from the multicast delegate, leaving byeDel,
// which calls only the method Goodbye.
multiMinusHiDel = multiDel - hiDel;
Console.WriteLine("Invoking delegate hiDel:");
hiDel("A");
Console.WriteLine("Invoking delegate byeDel:");
byeDel("B");
Console.WriteLine("Invoking delegate multiDel:");
multiDel("C");
Console.WriteLine("Invoking delegate multiMinusHiDel:");
multiMinusHiDel("D");
}
}
輸出:
Invoking delegate hiDel: Hello, A! Invoking delegate byeDel: Goodbye, B! Invoking delegate multiDel: Hello, C! Goodbye, C! Invoking delegate multiMinusHiDel: Goodbye, D!
聲明、實(shí)例化和使用委托
在 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 提供了更簡(jiǎn)單的方法來編寫上面的聲明,如以下示例所示。
// 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 及更高版本中,還可以使用 Lambda 表達(dá)式來聲明和實(shí)例化委托,如以下示例所示。
// Instantiate Del by using a lambda expression.
Del del4 = name => { Console.WriteLine("Notification received for: {0}", name); };
下面的示例闡釋聲明、實(shí)例化和使用委托。 BookDB 類封裝一個(gè)書店數(shù)據(jù)庫(kù),它維護(hù)一個(gè)書籍?dāng)?shù)據(jù)庫(kù)。它公開 ProcessPaperbackBooks 方法,該方法在數(shù)據(jù)庫(kù)中查找所有平裝書,并對(duì)每本平裝書調(diào)用一個(gè)委托。使用的 delegate 類型名為 ProcessBookDelegate。 Test 類使用該類打印平裝書的書名和平均價(jià)格。
委托的使用促進(jìn)了書店數(shù)據(jù)庫(kù)和客戶代碼之間功能的良好分隔??蛻舸a不知道書籍的存儲(chǔ)方式和書店代碼查找平裝書的方式。書店代碼也不知道找到平裝書后將對(duì)平裝書執(zhí)行什么處理。
// 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);
}
}
}
輸出:
Paperback Book Titles: The C Programming Language The Unicode Standard 2.0 Dogbert's Clues for the Clueless Average Paperback Book Price: $23.97
可靠編程
聲明委托。
下面的語(yǔ)句聲明一個(gè)新的委托類型。
public delegate void ProcessBookDelegate(Book book);
每個(gè)委托類型都描述參數(shù)的數(shù)目和類型,以及它可以封裝的方法的返回值類型。每當(dāng)需要一組新的參數(shù)類型或新的返回值類型時(shí),都必須聲明一個(gè)新的委托類型。
實(shí)例化委托。
聲明了委托類型后,必須創(chuàng)建委托對(duì)象并使之與特定方法關(guān)聯(lián)。在上一個(gè)示例中,您通過按下面示例中的方式將 PrintTitle 方法傳遞到 ProcessPaperbackBooks 方法來實(shí)現(xiàn)這一點(diǎn):
bookDB.ProcessPaperbackBooks(PrintTitle);
這將創(chuàng)建與靜態(tài)方法 Test.PrintTitle 關(guān)聯(lián)的新委托對(duì)象。類似地,對(duì)象 totaller 的非靜態(tài)方法 AddBookToTotal 是按下面示例中的方式傳遞的:
bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);
在兩個(gè)示例中,都向 ProcessPaperbackBooks 方法傳遞了一個(gè)新的委托對(duì)象。
委托創(chuàng)建后,它的關(guān)聯(lián)方法就不能更改;委托對(duì)象是不可變的。
調(diào)用委托。
創(chuàng)建委托對(duì)象后,通常將委托對(duì)象傳遞給將調(diào)用該委托的其他代碼。通過委托對(duì)象的名稱(后面跟著要傳遞給委托的參數(shù),括在括號(hào)內(nèi))調(diào)用委托對(duì)象。下面是委托調(diào)用的示例:
processBook(b);
與本例一樣,可以通過使用 BeginInvoke 和 EndInvoke 方法同步或異步調(diào)用委托。
相關(guān)文章
C#調(diào)用barTender打印標(biāo)簽示例的實(shí)現(xiàn)
Bartender是最優(yōu)秀的條碼打印軟件,在企業(yè)里使用非常普遍,本文主要介紹了C#調(diào)用barTender打印標(biāo)簽示例的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
C#中實(shí)現(xiàn)抽象類里建立靜態(tài)方法
這篇文章主要介紹了C#中實(shí)現(xiàn)抽象類里建立靜態(tài)方法,需要的朋友可以參考下2014-07-07
C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問
本文詳細(xì)講解了C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
C# BeginInvoke實(shí)現(xiàn)異步編程方式
這篇文章主要介紹了C# BeginInvoke實(shí)現(xiàn)異步編程方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

