C#自定义泛型类

转载自:C#自定义泛型类

C#自定义泛型类用得最广泛,就是集合(Collection)中。实际上,泛型的产生其中一个原因就是为了解决原来集合类中元素的装箱和拆箱问题(如果对装箱和拆箱概念不明,请百度搜索)。由于泛型的使用,使得集合内所有元素都属于同一类,这就把类型不同的隐患消灭在编译阶段——如果类型不对,则编译错误。

 

这里只讨论C#自定义泛型类。

基本自定义如下

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MyGeneric < T>    
  2. {    
  3.     private T member;    
  4.     public void Method (T obj)    
  5.     {    
  6.     }    
  7. }  


这里,定义了一个泛型类,其中的T作为一个类,可以在定义的类中使用。当然,要定义多个泛型类,也没有问题。

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MyGeneric < TKey, TValue>    
  2. {    
  3.      private TKey key;    
  4.      private TValue value;    
  5.    
  6.      public void Method (TKey k, TValue v)    
  7.      {    
  8.      }    
  9. }    

泛型的初始化:泛型是需要进行初始化的。使用Tdoc = default(T)以后,系统会自动为泛型进行初始化。

 

限制:如果我们知道,这个将要传入的泛型类T,必定具有某些的属性,那么我们就可以在MyGeneric<T>中使用T的这些属性。这一点,是通过interface来实现的。

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 先定义一个interface    
  2. public interface IDocument    
  3. {    
  4.    string Title {get;}    
  5.    string Content {get;}    
  6. }    
  7.    
  8. // 让范型类T实现这个interface    
  9. public class MyGeneric < T>    
  10. where T : IDocument    
  11. {    
  12.      public void Method(T v)    
  13.      {    
  14.          Console.WriteLine(v.Title);    
  15.      }    
  16. }    
  17.    
  18. // 传入的类也必须实现interface    
  19. public class Document : IDocument    
  20. {    
  21. }    
  22.    
  23. // 使用这个泛型    
  24. MyGeneric< Document> doc = new MyGeneric< Document>();   

泛型方法:我们同样可以定义泛型的方法

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void Swap< T> (ref T x, ref T y)    
  2. {    
  3.     T temp = x;    
  4.     x = y;    
  5.     y = temp;    
  6. }    

泛型代理(GenericDelegate):既然能够定义泛型方法,自然也可以定义泛型代理

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public delegate void delegateSample < T> (ref T x, ref T y)    
  2.    
  3. private void Swap (ref T x, ref T y)    
  4. {    
  5.     T temp = x;    
  6.     x = y;    
  7.     y = temp;    
  8. }    
  9.    
  10. // 调用    
  11. public void Run()    
  12. {    
  13.    int i,j;    
  14.    i = 3;    
  15.    j = 5;    
  16.    delegateSample< int> sample = new delegateSample< int> (Swap);    
  17.    sample(i, j);    
  18. }   

设置可空值类型:一般来说,值类型的变量是非空的。但是,Nullable<T>可以解决这个问题
 

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Nullable< int>x;   // 这样就设置了一个可空的整数变量x   
  2. x = 4;   
  3. x += 3;   
  4. if (x.HasValue)   // 使用HasValue属性来检查x是否为空   
  5. {  
  6. Console.WriteLine("x="+x.ToString());   
  7. }   
  8. x = null;    // 可设空值  
  9.    

使用ArraySegment<T>来获得数组的一部分。如果要使用一个数组的部分元素,直接使用ArraySegment来圈定不失为一个不错的办法

 

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int[] arr = ...{1, 2,3, 4, 5, 6, 7, 8, 9};   
  2. //第一个参数是传递数组,第二个参数是起始段在数组内的偏移,第三个参数是要取连续多少个数   
  3. ArraySegment<int> segment = new ArraySegment< int>(arr, 2, 3);  // (array, offset, count)    
  4.    
  5. for (int i =segment.Offset; i< = segment.Offset + segment.Count; i++)   
  6. {   
  7.    Console.WriteLine(segment.Array[i]);    // 使用Array属性来访问传递的数组   
  8. }  

在例子中,通过将Offset属性和Count属性设置为不同的值,可以达到访问不同段的目的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于C#编程语言的问题,下面是答案: 我们可以创建一个名为Calculator的自定义泛型类,并在其中定义一个Add泛型方法。这个方法可以接受任意类型的参数,并计算两个参数的和。以下是代码实现: ``` public class Calculator<T> { public T Add(T num1, T num2) { dynamic dynamicNum1 = num1; dynamic dynamicNum2 = num2; return dynamicNum1 + dynamicNum2; } } ``` 在上面的代码中,我们使用了C# 4.0中引入的dynamic类型。这个类型可以在运行时决定变量的类型,从而允许我们对任意类型的参数进行加法操作。在Add方法中,我们首先将num1和num2转换为dynamic类型,并将它们相加。最后,我们将结果返回。 现在,我们可以使用这个自定义泛型类Calculator来计算任意类型的两个数的和。以下是使用示例: ``` Calculator<int> intCalculator = new Calculator<int>(); int sum1 = intCalculator.Add(5, 10); // sum1 = 15 Calculator<double> doubleCalculator = new Calculator<double>(); double sum2 = doubleCalculator.Add(3.14, 2.71); // sum2 = 5.85 Calculator<string> stringCalculator = new Calculator<string>(); string sum3 = stringCalculator.Add("Hello", " World"); // sum3 = "Hello World" ``` 在上面的代码中,我们分别使用了int、double和string类型来创建三个不同的Calculator对象,并调用了它们的Add方法来计算两个数的和。可以看到,这个自定义泛型类可以非常方便地处理任意类型的参数,从而使我们的代码更加灵活和可重用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值