开发手册 欢迎您!
软件开发者资料库

.NET Core(C#)泛型(方法,类,委托,接口)<T>使用示例代码

我们可以利用泛型实现:泛型接口、泛型方法、泛型类、泛型委托,本文主要它们的使用及示例代码。

1、使用示例代码

1)泛型(类、接口、委托)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MyGeneric{    ///     /// 一个类来满足不同的具体类型,做相同的事儿    ///     ///     ///     ///     ///     ///     public class GenericClass        //, S, X, Eleven, 老K>        //where T : People        //where S : Chinese        //where Eleven : Hubei    {        public T _T;    }    ///     ///  一个接口来满足不同的具体类型的接口,做相同的事儿    ///     ///     public interface IGenericInterface //where T : People    {        T GetT(T t);//泛型类型的返回值    }    public class CommonClass        //: GenericClass//必须指定        : IGenericInterface//必须指定    {        public int GetT(int t)        {            throw new NotImplementedException();        }    }    public class GenericClassChild        //: GenericClass        : GenericClass, IGenericInterface    {        public Eleven GetT(Eleven t)        {            throw new NotImplementedException();        }    }    public delegate void SayHi(T t);//泛型委托}

2) 泛型方法

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MyGeneric{    ///     /// 泛型方法    ///     public class GenericMethod    {        ///         /// 2.0推出的新语法        /// 泛型方法解决用一个方法,满足不同参数类型;做相同的事儿        /// 没有写死参数类型,调用的时候才指定的类型        ///         /// T/S 不要用关键字  也不要跟别的类型冲突         ///         public static void Show(T tParameter)        {            Console.WriteLine("This is {0},parameter={1},type={2}",                typeof(GenericMethod), tParameter.GetType().Name, tParameter.ToString());        }        public static void ShowObject(object oParameter)        {            Console.WriteLine("This is {0},parameter={1},type={2}",                typeof(GenericMethod), oParameter.GetType().Name, oParameter);        }    }}

2、泛型类介绍

泛型类封装了不针对任何特定数据类型的操作。泛型类常用于容器类,如链表、哈希表、栈、队列、树等等。这些类中的操作,如对容器添加、删除元素,不论所存储的数据是何种类型,都执行几乎同样的操作。

对大多数情况,推荐使用.NET框架2.0类库中所提供的容器类。有关使用这些类的详细信息,请参见基础类库中的泛型。

通常,从一个已有的具体类来创建泛型类,并每次把一个类型改为类型参数,直至达到一般性和可用性的最佳平衡。当创建你自己的泛型类时,需要重点考虑的事项有:

       哪些类型应泛化为类型参数。一般的规律是,用参数表示的类型越多,代码的灵活性和复用性也就越大。过多的泛化会导致代码难以被其它的开发人员理解。

       如果有约束,那么类型参数需要什么样约束。一个良好的习惯是,尽可能使用最大的约束,同时保证可以处理所有需要处理的类型。例如,如果你知道你的泛型类只打算使用引用类型,那么就应用这个类的约束。这样可以防止无意中使用值类型,同时可以对T使用as运算符,并且检查空引用。

       把泛型行为放在基类中还是子类中。泛型类可以做基类。同样非泛型类的设计中也应考虑这一点。泛型基类的继承规则     。

      是否实现一个或多个泛型接口。例如,要设计一个在基于泛型的容器中创建元素的类,可能需要实现类似IComparable的接口,其中T是该类的参数。

//泛型类:public class MySQLHelp{       private T t;       public MySQLHelp(T t)        {            this.t = t;        }    }//测试类public class Test{     public static void Main(){               MySQLHelp mm = new MySQLHelp(new Message());     }}//其他类public class Message{}

3、泛型接口介绍

不论是为泛型容器类,还是表示容器中元素的泛型类,定义接口是很有用的。把泛型接口与泛型类结合使用是更好的用法,比如用IComparable而非IComparable,以避免值类型上的装箱和拆箱操作。.NET框架2.0类库定义了几个新的泛型接口,以配合System.Collections.Generic中新容器类的使用。

    当一个接口被指定为类型参数的约束时,只有实现该接口的类型可被用作类型参数。下面的示例代码显示了一个从MyList派生的SortedList类。更多信息,请参见泛型概述。SortedList增加了约束where T : IComparable

这使得SortedList中的BubbleSort方法可以使用表中的元素的IComparable.CompareTo方法。在这个例子中,表中的元素是简单类——实现IComparablePerson类。

using System;using System.Collections.Generic; //Type parameter T in angle brackets.public class MyList{    protected Node head;    protected Node current = null; // Nested type is also generic on T    protected class Node             {        public Node next;//T as private member datatype.        private T data;         //T used in non-generic constructor.        public Node(T t)                {            next = null;            data = t;        }        public Node Next        {            get { return next; }            set { next = value; }        }//T as return type of property.        public T Data                   {            get { return data; }            set { data = value; }        }    }    public MyList()    {        head = null;    }//T as method parameter type.    public void AddHead(T t)        {        Node n = new Node(t);        n.Next = head;        head = n;       }    // Implement IEnumerator to enable foreach    // iteration of our list. Note that in C# 2.0    // you are not required to implment Current and    // GetNext. The compiler does that for you.    public IEnumerator GetEnumerator()    {        Node current = head;         while (current != null)        {            yield return current.Data;            current = current.Next;        }    }}  public class SortedList : MyList where T : IComparable{    // A simple, unoptimized sort algorithm that    // orders list elements from lowest to highest: public void BubbleSort()    {         if (null == head || null == head.Next)            return;        bool swapped;         do        {            Node previous = null;            Node current = head;            swapped = false;             while (current.next != null)            {                //  Because we need to call this method, the SortedList                //  class is constrained on IEnumerable                if (current.Data.CompareTo(current.next.Data) > 0)                {                    Node tmp = current.next;                    current.next = current.next.next;                    tmp.next = current;                     if (previous == null)                    {                        head = tmp;                    }                    else                    {                        previous.next = tmp;                    }                    previous = tmp;                    swapped = true;                }                 else                {                    previous = current;                    current = current.next;                }             }// end while        } while (swapped);    } } // A simple class that implements IComparable// using itself as the type argument. This is a// common design pattern in objects that are// stored in generic lists.public class Person : IComparable{    string name;    int age;    public Person(string s, int i)    {        name = s;        age = i;    }    // This will cause list elements    // to be sorted on age values.    public int CompareTo(Person p)    {        return age - p.age;    }    public override string ToString()    {        return name + ":" + age;    }    // Must implement Equals.    public bool Equals(Person p)    {        return (this.age == p.age);    }} class Program{    static void Main(string[] args)    {        //Declare and instantiate a new generic SortedList class.        //Person is the type argument.        SortedList list = new SortedList();         //Create name and age values to initialize Person objects.        string[] names = new string[]{"Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul"};        int[] ages = new int[]{45, 19, 28, 23, 18, 9, 108, 72, 30, 35};         //Populate the list.        for (int x = 0; x < 10; x++)        {            list.AddHead(new Person(names[x], ages[x]));        }        //Print out unsorted list.        foreach (Person p in list)        {            Console.WriteLine(p.ToString());        }         //Sort the list.        list.BubbleSort();         //Print out sorted list.        foreach (Person p in list)        {            Console.WriteLine(p.ToString());        }         Console.WriteLine("Done");    }}

可以在一个类型指定多个接口作为约束,如下:

class Stack where T : IComparable, IMyStack1{}

一个接口可以定义多个类型参数,如下:

IDictionary

接口和类的继承规则相同:

IMyInterface : IBaseInterface
IMyInterface : IBaseInterface
IMyInterface: IBaseInterface

 

具体类可以实现封闭构造接口,如下:

class MyClass : IBaseInterface

泛型类可以实现泛型接口或封闭构造接口,只要类的参数列表提供了接口需要的所有参数,如下:

class MyClass : IBaseInterface
class MyClass : IBaseInterface

4、泛型方法介绍

泛型方法是声名了类型参数的方法,如下:

void Swap( ref T lhs, ref T rhs){  T temp;  temp = lhs;  lhs = rhs;  rhs = temp;}

下面的示例代码显示了一个以int作为类型参数,来调用方法的例子: 

int a = 1;int b = 2;//…Swap(a, b);

也可以忽略类型参数,编译器会去推断它。下面调用Swap的代码与上面的例子等价:

Swap(a, b);

静态方法和实例方法有着同样的类型推断规则。编译器能够根据传入的方法参数来推断类型参数;而无法单独根据约束或返回值来判断。因此类型推断对没有参数的方法是无效的。类型推断发生在编译的时候,且在编译器解析重载方法标志之前。编译器对所有同名的泛型方法应用类型推断逻辑。在决定(resolution)重载的阶段,编译器只包含那些类型推断成功的泛型类。更多信息,请参见C# 2.0规范,20.6.4类型参数推断

在泛型方法中,非泛型方法能访问所在类中的类型参数,如下:

class MyClass{  //…  void Swap (ref T lhs, ref T rhs){…}}

[JX1] 定义一个泛型方法,和其所在的类具有相同的类型参数;试图这样做,编译器会产生警告CS0693。

class MyList{// CS0693    void MyMethod{...}   } class MyList{//This is okay, but not common.    void SomeMethod(){...}   }

使用约束可以在方法中使用更多的类型参数的特定方法。这个版本的Swap称为SwapIfGreater,它只能使用实现了IComparable的类型参数。

void SwapIfGreater( ref T lhs, ref T rhs) where T: IComparable{  T temp;  if(lhs.CompareTo(rhs) > 0)    {      temp = lhs;      lhs = rhs;      rhs = temp;    }}

泛型方法通过多个类型参数来重载。例如,下面的这些方法可以放在同一个类中:

void DoSomething(){}void DoSomething(){}void DoSomething(){} 

5、泛型委托介绍

无论是在类定义内还是类定义外,委托可以定义自己的类型参数。引用泛型委托的代码可以指定类型参数来创建一个封闭构造类型,这和实例化泛型类或调用泛型方法一样,如下例所示:

public delegate void MyDelegate(T item);public void Notify(int i){}//... MyDelegate m = new MyDelegate(Notify);

C#2.0版有个新特性称为方法组转换(method group conversion),具体代理和泛型代理类型都可以使用。用方法组转换可以把上面一行写做简化语法:

MyDelegate m = Notify; 

在泛型类中定义的委托,可以与类的方法一样地使用泛型类的类型参数。

class Stack{T[] items;      int index//...public delegate void StackDelegate(T[] items);}

引用委托的代码必须要指定所在类的类型参数,如下:

Stack s = new Stack();Stack.StackDelegate myDelegate = StackNotify;

泛型委托在定义基于典型设计模式的事件时特别有用。因为sender[JX2] ,而再也不用与Object相互转换。

public void StackEventHandler(T sender, U eventArgs);class Stack{    //…    public class StackEventArgs : EventArgs{...}    public event StackEventHandler, StackEventArgs> stackEvent;    protected virtual void OnStackChanged(StackEventArgs a)    {      stackEvent(this, a);    }}class MyClass{  public static void HandleStackChange(Stack stack, StackEventArgs args){...};}Stack s = new Stack();MyClass mc = new MyClass();s.StackEventHandler += mc.HandleStackChange;

相关文档

.NET Core(C#)泛型中约束的使用及示例代码

C#(.NET Core) 泛型中协变(covariant)和逆变(contravariant)的使用