C# -泛型



泛型允許您定義類或方法中程式設計元素的資料型別規範,直到它實際在程式中使用。換句話說,泛型允許您編寫可以處理任何資料型別的類或方法。

您可以使用資料型別的替代引數編寫類或方法的規範。當編譯器遇到類的建構函式或方法的函式呼叫時,它會生成程式碼來處理特定資料型別。一個簡單的例子將有助於理解這個概念:

using System;
using System.Collections.Generic;

namespace GenericApplication {
   public class MyGenericArray<T> {
      private T[] array;
      
      public MyGenericArray(int size) {
         array = new T[size + 1];
      }
      public T getItem(int index) {
         return array[index];
      }
      public void setItem(int index, T value) {
         array[index] = value;
      }
   }
   class Tester {
      static void Main(string[] args) {
         
         //declaring an int array
         MyGenericArray<int> intArray = new MyGenericArray<int>(5);
         
         //setting values
         for (int c = 0; c < 5; c++) {
            intArray.setItem(c, c*5);
         }
         
         //retrieving the values
         for (int c = 0; c < 5; c++) {
            Console.Write(intArray.getItem(c) + " ");
         }
         
         Console.WriteLine();
         
         //declaring a character array
         MyGenericArray<char> charArray = new MyGenericArray<char>(5);
         
         //setting values
         for (int c = 0; c < 5; c++) {
            charArray.setItem(c, (char)(c+97));
         }
         
         //retrieving the values
         for (int c = 0; c< 5; c++) {
            Console.Write(charArray.getItem(c) + " ");
         }
         Console.WriteLine();
         
         Console.ReadKey();
      }
   }
}

當上述程式碼編譯並執行時,它會產生以下結果:

0 5 10 15 20
a b c d e

泛型的特性

泛型是一種可以以下列方式豐富您的程式的技術:

  • 它幫助您最大限度地提高程式碼重用性、型別安全性和效能。

  • 您可以建立泛型集合類。.NET Framework類庫在System.Collections.Generic名稱空間中包含幾個新的泛型集合類。您可以使用這些泛型集合類代替System.Collections名稱空間中的集合類。

  • 您可以建立自己的泛型介面、類、方法、事件和委託。

  • 您可以建立約束泛型類以啟用對特定資料型別上的方法的訪問。

  • 您可以透過反射在執行時獲取有關在泛型資料型別中使用的型別的資訊。

泛型方法

在前面的示例中,我們使用了泛型類;我們可以使用型別引數宣告泛型方法。下面的程式說明了這個概念:

using System;
using System.Collections.Generic;

namespace GenericMethodAppl {
   class Program {
      static void Swap<T>(ref T lhs, ref T rhs) {
         T temp;
         temp = lhs;
         lhs = rhs;
         rhs = temp;
      }
      static void Main(string[] args) {
         int a, b;
         char c, d;
         a = 10;
         b = 20;
         c = 'I';
         d = 'V';
         
         //display values before swap:
         Console.WriteLine("Int values before calling swap:");
         Console.WriteLine("a = {0}, b = {1}", a, b);
         Console.WriteLine("Char values before calling swap:");
         Console.WriteLine("c = {0}, d = {1}", c, d);
         
         //call swap
         Swap<int>(ref a, ref b);
         Swap<char>(ref c, ref d);
         
         //display values after swap:
         Console.WriteLine("Int values after calling swap:");
         Console.WriteLine("a = {0}, b = {1}", a, b);
         Console.WriteLine("Char values after calling swap:");
         Console.WriteLine("c = {0}, d = {1}", c, d);
         
         Console.ReadKey();
      }
   }
}

當上述程式碼編譯並執行時,它會產生以下結果:

Int values before calling swap:
a = 10, b = 20
Char values before calling swap:
c = I, d = V
Int values after calling swap:
a = 20, b = 10
Char values after calling swap:
c = V, d = I

泛型委託

您可以使用型別引數定義泛型委託。例如:

delegate T NumberChanger<T>(T n);

下面的示例顯示了此委託的用法:

using System;
using System.Collections.Generic;

delegate T NumberChanger<T>(T n);
namespace GenericDelegateAppl {
   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) {
         //create delegate instances
         NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
         NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
         
         //calling the methods using the delegate objects
         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
廣告