转载自:C#自定义泛型类
C#自定义泛型类用得最广泛,就是集合(Collection)中。实际上,泛型的产生其中一个原因就是为了解决原来集合类中元素的装箱和拆箱问题(如果对装箱和拆箱概念不明,请百度搜索)。由于泛型的使用,使得集合内所有元素都属于同一类,这就把类型不同的隐患消灭在编译阶段——如果类型不对,则编译错误。
这里只讨论C#自定义泛型类。
基本自定义如下:
- public class MyGeneric < T>
- {
- private T member;
- public void Method (T obj)
- {
- }
- }
这里,定义了一个泛型类,其中的T作为一个类,可以在定义的类中使用。当然,要定义多个泛型类,也没有问题。
- public class MyGeneric < TKey, TValue>
- {
- private TKey key;
- private TValue value;
-
- public void Method (TKey k, TValue v)
- {
- }
- }
泛型的初始化:泛型是需要进行初始化的。使用Tdoc = default(T)以后,系统会自动为泛型进行初始化。
限制:如果我们知道,这个将要传入的泛型类T,必定具有某些的属性,那么我们就可以在MyGeneric<T>中使用T的这些属性。这一点,是通过interface来实现的。
-
- public interface IDocument
- {
- string Title {get;}
- string Content {get;}
- }
-
-
- public class MyGeneric < T>
- where T : IDocument
- {
- public void Method(T v)
- {
- Console.WriteLine(v.Title);
- }
- }
-
-
- public class Document : IDocument
- {
- }
-
-
- MyGeneric< Document> doc = new MyGeneric< Document>();
泛型方法:我们同样可以定义泛型的方法
- void Swap< T> (ref T x, ref T y)
- {
- T temp = x;
- x = y;
- y = temp;
- }
泛型代理(GenericDelegate):既然能够定义泛型方法,自然也可以定义泛型代理
- public delegate void delegateSample < T> (ref T x, ref T y)
-
- private void Swap (ref T x, ref T y)
- {
- T temp = x;
- x = y;
- y = temp;
- }
-
-
- public void Run()
- {
- int i,j;
- i = 3;
- j = 5;
- delegateSample< int> sample = new delegateSample< int> (Swap);
- sample(i, j);
- }
设置可空值类型:一般来说,值类型的变量是非空的。但是,Nullable<T>可以解决这个问题
- Nullable< int>x;
- x = 4;
- x += 3;
- if (x.HasValue)
- {
- Console.WriteLine("x="+x.ToString());
- }
- x = null;
-
使用ArraySegment<T>来获得数组的一部分。如果要使用一个数组的部分元素,直接使用ArraySegment来圈定不失为一个不错的办法
- int[] arr = ...{1, 2,3, 4, 5, 6, 7, 8, 9};
-
- ArraySegment<int> segment = new ArraySegment< int>(arr, 2, 3);
-
- for (int i =segment.Offset; i< = segment.Offset + segment.Count; i++)
- {
- Console.WriteLine(segment.Array[i]);
- }
在例子中,通过将Offset属性和Count属性设置为不同的值,可以达到访问不同段的目的。