C# - 泛型

C# - 泛型 首页 / C#入门教程 / 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();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-generics.html

来源:LearnFk无涯教程网

0 5 10 15 20
a b c d e

泛型方法

在前面的示例中,我们使用了泛型类,我们可以使用类型参数声明泛型方法。以下程序说明了概念

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();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-generics.html

来源:LearnFk无涯教程网

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();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-generics.html

来源:LearnFk无涯教程网

Value of Num: 35
Value of Num: 175

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言核心36讲 -〔郝林〕

从0开始学微服务 -〔胡忠想〕

Go语言从入门到实战 -〔蔡超〕

iOS开发高手课 -〔戴铭〕

浏览器工作原理与实践 -〔李兵〕

人人都能学会的编程入门课 -〔胡光〕

架构实战案例解析 -〔王庆友〕

微信小程序全栈开发实战 -〔李艺〕

说透数字化转型 -〔付晓岩〕

好记忆不如烂笔头。留下您的足迹吧 :)