国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - 舉例講解C#編程中委托的實例化使用

舉例講解C#編程中委托的實例化使用

2021-11-11 13:38C#教程網 C#

這篇文章主要介紹了C#編程中委托的實例化使用,包括委托的聲明和多播委托的創建等內容,需要的朋友可以參考下

合并委托
本示例演示如何創建多播委托。 委托對象的一個有用屬性是:可以使用 + 運算符將多個對象分配給一個委托實例。多播委托包含已分配委托的列表。在調用多播委托時,它會按順序調用列表中的委托。只能合并相同類型的委托。
- 運算符可用于從多播委托中移除組件委托。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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");
  }
}

輸出:

?
1
2
3
4
5
6
7
8
9
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 及更高版本中,可以按以下示例所示聲明委托。


 

?
1
2
3
4
5
6
7
8
9
10
11
12
// 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 提供了更簡單的方法來編寫上面的聲明,如以下示例所示。

?
1
2
// C# 2.0 provides a simpler way to declare an instance of Del.
Del del2 = Notify;

在 C# 2.0 及更高版本中,還可以使用匿名方法來聲明和初始化委托,如以下示例所示。

?
1
2
3
// Instantiate Del by using an anonymous method.
Del del3 = delegate(string name)
  { Console.WriteLine("Notification received for: {0}", name); };

在 C# 3.0 及更高版本中,還可以使用 Lambda 表達式來聲明和實例化委托,如以下示例所示。

?
1
2
// Instantiate Del by using a lambda expression.
Del del4 = name => { Console.WriteLine("Notification received for: {0}", name); };

下面的示例闡釋聲明、實例化和使用委托。 BookDB 類封裝一個書店數據庫,它維護一個書籍數據庫。它公開 ProcessPaperbackBooks 方法,該方法在數據庫中查找所有平裝書,并對每本平裝書調用一個委托。使用的 delegate 類型名為 ProcessBookDelegate。 Test 類使用該類打印平裝書的書名和平均價格。
委托的使用促進了書店數據庫和客戶代碼之間功能的良好分隔。客戶代碼不知道書籍的存儲方式和書店代碼查找平裝書的方式。書店代碼也不知道找到平裝書后將對平裝書執行什么處理。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 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);
    }
  }
}

輸出:

?
1
2
3
4
5
Paperback Book Titles:
  The C Programming Language
  The Unicode Standard 2.0
  Dogbert's Clues for the Clueless
Average Paperback Book Price: $23.97

可靠編程
聲明委托。
下面的語句聲明一個新的委托類型。

?
1
public delegate void ProcessBookDelegate(Book book);

每個委托類型都描述參數的數目和類型,以及它可以封裝的方法的返回值類型。每當需要一組新的參數類型或新的返回值類型時,都必須聲明一個新的委托類型。
實例化委托。
聲明了委托類型后,必須創建委托對象并使之與特定方法關聯。在上一個示例中,您通過按下面示例中的方式將 PrintTitle 方法傳遞到 ProcessPaperbackBooks 方法來實現這一點:

?
1
bookDB.ProcessPaperbackBooks(PrintTitle);

這將創建與靜態方法 Test.PrintTitle 關聯的新委托對象。類似地,對象 totaller 的非靜態方法 AddBookToTotal 是按下面示例中的方式傳遞的:

?
1
bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal);

在兩個示例中,都向 ProcessPaperbackBooks 方法傳遞了一個新的委托對象。
委托創建后,它的關聯方法就不能更改;委托對象是不可變的。
調用委托。
創建委托對象后,通常將委托對象傳遞給將調用該委托的其他代碼。通過委托對象的名稱(后面跟著要傳遞給委托的參數,括在括號內)調用委托對象。下面是委托調用的示例:

?
1
processBook(b);

與本例一樣,可以通過使用 BeginInvoke 和 EndInvoke 方法同步或異步調用委托。

延伸 · 閱讀

精彩推薦
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
主站蜘蛛池模板: 神马久久久久久久 | 美女视频一区二区三区 | 国产综合在线播放 | 中文在线视频 | 亚洲免费视频网 | 亚洲精品久久久久久一区二区 | 欧美一级二级三级视频 | 国产色| 日韩性视频 | 美女久久| 高清一区二区在线观看 | 精品免费视频 | 国产伦精品一区二区三区 | 成人在线免费视频 | 欧美在线视频网站 | 免费成人av网站 | 久久久一区二区三区 | 日本久久精品一区 | 四虎影院在线 | 国产精品美女久久久久久久久久久 | 我和我的祖国电影在线观看免费版高清 | 久久久久亚洲 | 国产精品永久免费自在线观看 | 国产一区视频网站 | 国产高清在线a视频大全 | 色偷偷888欧美精品久久久 | 国产精品一二三区 | 国产精品a久久久久 | 亚洲一区二区精品视频 | 亚洲中字幕 | 午夜精品福利电影 | 国产黄色电影 | 婷婷色国产偷v国产偷v小说 | 91久久久久久久久 | 精品免费久久久久 | 亚洲精品日本 | 日本在线不卡视频 | 亚洲视频免费观看 | 亚洲成av人片在线观看 | 亚洲精品综合 | 国产黄色一级大片 |