<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C# 委托(Delegate) C# 中的委托(Delegate)類似于 C 或 C++ 中函數的指針。**委托(Delegate)** 是存有對某個方法的引用的一種引用類型變量。引用可在運行時被改變。 委托(Delegate)特別用于實現事件和回調方法。所有的委托(Delegate)都派生自 **System.Delegate** 類。 ## 聲明委托(Delegate) 委托聲明決定了可由該委托引用的方法。委托可指向一個與其具有相同標簽的方法。 例如,假設有一個委托: ``` public delegate int MyDelegate (string s); ``` 上面的委托可被用于引用任何一個帶有一個單一的 _string_ 參數的方法,并返回一個 _int_ 類型變量。 聲明委托的語法如下: ``` delegate <return type> <delegate-name> <parameter list> ``` ## 實例化委托(Delegate) 一旦聲明了委托類型,委托對象必須使用 **new** 關鍵字來創建,且與一個特定的方法有關。當創建委托時,傳遞到 **new** 語句的參數就像方法調用一樣書寫,但是不帶有參數。例如: ``` public delegate void printString(string s); ... printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile); ``` 下面的實例演示了委托的聲明、實例化和使用,該委托可用于引用帶有一個整型參數的方法,并返回一個整型值。 ``` using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } static void Main(string[] args) { // 創建委托實例 NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); // 使用委托對象調用方法 nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` Value of Num: 35 Value of Num: 175 ``` ## 委托的多播(Multicasting of a Delegate) 委托對象可使用 "+" 運算符進行合并。一個合并委托調用它所合并的兩個委托。只有相同類型的委托可被合并。"-" 運算符可用于從合并的委托中移除組件委托。 使用委托的這個有用的特點,您可以創建一個委托被調用時要調用的方法的調用列表。這被稱為委托的 **多播(multicasting)**,也叫組播。下面的程序演示了委托的多播: ``` using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } static void Main(string[] args) { // 創建委托實例 NumberChanger nc; NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); nc = nc1; nc += nc2; // 調用多播 nc(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` Value of Num: 75 ``` ## 委托(Delegate)的用途 下面的實例演示了委托的用法。委托 _printString_ 可用于引用帶有一個字符串作為輸入的方法,并不返回任何東西。 我們使用這個委托來調用兩個方法,第一個把字符串打印到控制臺,第二個把字符串打印到文件: ``` using System; using System.IO; namespace DelegateAppl { class PrintString { static FileStream fs; static StreamWriter sw; // 委托聲明 public delegate void printString(string s); // 該方法打印到控制臺 public static void WriteToScreen(string str) { Console.WriteLine("The String is: {0}", str); } // 該方法打印到文件 public static void WriteToFile(string s) { fs = new FileStream("c:\\message.txt", FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs); sw.WriteLine(s); sw.Flush(); sw.Close(); fs.Close(); } // 該方法把委托作為參數,并使用它調用方法 public static void sendString(printString ps) { ps("Hello World"); } static void Main(string[] args) { printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile); sendString(ps1); sendString(ps2); Console.ReadKey(); } } } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` The String is: Hello World ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看