⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-12-03

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 12-03: Combining delegates

namespace Programming_CSharp
{
   using System;

   public class MyClassWithDelegate
   {
      // the delegate declaration
      public delegate void StringDelegate(string s);

   }

   public class MyImplementingClass
   {
      public static void WriteString(string s)
      {
         Console.WriteLine("Writing string {0}", s);
      }

      public static void LogString(string s)
      {
         Console.WriteLine("Logging string {0}", s);
      }

      public static void TransmitString(string s)
      {
         Console.WriteLine("Transmitting string {0}", s);
      }

   }


   public class Test
   {
      public static void Main()
      {
         // define three StringDelegate objects
         MyClassWithDelegate.StringDelegate 
            Writer, Logger, Transmitter;

         // define another StringDelegate
         // to act as the multicast delegate
         MyClassWithDelegate.StringDelegate 
            myMulticastDelegate;

         // Instantiate the first three delegates, 
         // passing in methods to encapsulate
         Writer = new MyClassWithDelegate.StringDelegate(
            MyImplementingClass.WriteString);
         Logger = new MyClassWithDelegate.StringDelegate(
            MyImplementingClass.LogString);
         Transmitter = 
            new MyClassWithDelegate.StringDelegate(
            MyImplementingClass.TransmitString);

         // Invoke the Writer delegate method
         Writer("String passed to Writer\n");

         // Invoke the Logger delegate method 
         Logger("String passed to Logger\n");

         // Invoke the Transmitter delegate method
         Transmitter("String passed to Transmitter\n");
            
         // Tell the user you are about to combine
         // two delegates into the multicast delegate
         Console.WriteLine(
            "myMulticastDelegate = Writer + Logger");

         // combine the two delegates, the result is
         // assigned to myMulticast Delegate
         myMulticastDelegate = Writer + Logger;

         // Call the delegated methods, two methods
         // will be invoked
         myMulticastDelegate(
            "First string passed to Collector");

         // Tell the user you are about to add
         // a third delegate to the multicast
         Console.WriteLine(
            "\nmyMulticastDelegate += Transmitter");

         // add the third delegate
         myMulticastDelegate += Transmitter;

         // invoke the three delegated methods
         myMulticastDelegate(
            "Second string passed to Collector");

         // tell the user you are about to remove
         // the logger delegate
         Console.WriteLine(
            "\nmyMulticastDelegate -= Logger");

         // remove the logger delegate
         myMulticastDelegate -= Logger;

         // invoke the two remaining 
         // delegated methods
         myMulticastDelegate(
            "Third string passed to Collector");
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -