C# 中的泛型委託是什麼?

WBOY
發布: 2023-08-26 13:49:05
轉載
687 人瀏覽過

C# 中的泛型委托是什么?

使用通用委託,您不需要定義委託語句。它們在系統命名空間中定義。

您可以使用類型參數定義通用委託。例如 -

delegate T myDelegete<T>(T n);
登入後複製

範例

以下範例展示如何在 C# 中建立通用委託 -

using System;
using System.Collections.Generic;

delegate T myDelegete<T>(T n);
namespace GenericDelegateAppl {
   class TestDelegate {
      static int num = 5;

      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) {
         //create delegate instances
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);

         //calling the methods using the delegate objects
         nc1(50);
         Console.WriteLine("Value of Num: {0}", getNum());

         nc2(10);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}
登入後複製

以上是C# 中的泛型委託是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!