【c#教學】C# 泛型(Generic)
C# 泛型(Generic)
泛型(Generic) 允許您延遲編寫類別或方法中的程式設計元素的資料類型的規範,直到實際在程式中使用它的時候。換句話說,泛型允許您編寫一個可以與任何資料類型一起工作的類別或方法。
您可以透過資料類型的替代參數來編寫類別或方法的規格。當編譯器遇到類別的建構函式或方法的函式呼叫時,它會產生程式碼來處理指定的資料類型。下面這個簡單的實例將有助於您理解這個概念:
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) { // 声明一个整型数组 MyGenericArray<int> intArray = new MyGenericArray<int>(5); // 设置值 for (int c = 0; c < 5; c++) { intArray.setItem(c, c*5); } // 获取值 for (int c = 0; c < 5; c++) { Console.Write(intArray.getItem(c) + " "); } Console.WriteLine(); // 声明一个字符数组 MyGenericArray<char> charArray = new MyGenericArray<char>(5); // 设置值 for (int c = 0; c < 5; c++) { charArray.setItem(c, (char)(c+97)); } // 获取值 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
泛型(Generic)的特性
使用泛型是一種增強程式功能的技術,具體表現在以下幾個方面:
它有助於您最大限度地重複使用程式碼、保護類型的安全以及提高效能。
您可以建立泛型集合類別。 .NET 框架類別庫在 System.Collections.Generic 命名空間中包含了一些新的泛型集合類別。您可以使用這些泛型集合類別來取代 System.Collections 中的集合類別。
您可以建立自己的泛型介面、泛型類別、泛型方法、泛型事件和泛型委託。
您可以對泛型類別進行約束以存取特定資料類型的方法。
關於泛型資料類型中使用的類型的資訊可在運行時透過使用反射來取得。
泛型(Generic)方法
在上面的實例中,我們已經使用了泛型類,我們可以透過型別參數來宣告泛型方法。下面的程式說明了這個概念:
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'; // 在交换之前显示值 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); // 调用 swap Swap<int>(ref a, ref b); Swap<char>(ref c, ref d); // 在交换之后显示值 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
泛型(Generic)委託
您可以透過類型參數定義泛型委託。例如:
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) { // 创建委托实例 NumberChangernc1 = 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
型以上是【cc Generic)的內容,更多相關內容請關注PHP中文網(www.php.cn)!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。
