举例讲解C#编程中委托的实例化使用
合并委托
本示例演示如何创建多播委托。 委托对象的一个有用属性是:可以使用 + 运算符将多个对象分配给一个委托实例。多播委托包含已分配委托的列表。在调用多播委托时,它会按顺序调用列表中的委托。只能合并相同类型的委托。
- 运算符可用于从多播委托中移除组件委托。
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!
声明、实例化和使用委托
在 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 及更高版本中,还可以使用 lambda 表达式来声明和实例化委托,如以下示例所示。
// instantiate del by using a lambda expression. del del4 = name => { console.writeline("notification received for: {0}", name); };
下面的示例阐释声明、实例化和使用委托。 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); } } }
输出:
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#连接数据库的方法