C#泛型

1.泛型简介

   简要来说泛型其实就是对类的抽象,是比类型更抽象的一级。对类来说,类是具有相同属性和方法的一类对象的集合,也就是具有相同属性的对象的抽象。

而泛型是具有相同执行指令的类型的集合,泛型是这些相似类型的一个模板,是对他们的更高一级的抽象。

2.为什么使用泛型

    举例来说,现在有一个类,类中有一个某个类型的数组以及一个获取数组某索引的值的一个方法。

    代码如下:

 public class MyGeneric_int
    {
        int[] arr;
        public void SetArr(int[] arr)
        {
            this.arr = arr;
        }

        public int GetValue(int index)
        {
            if (index<arr.Length)
            {
                return arr[index];
            }
            else
            {
                throw new Exception("索引值超出了数组的界限");
            }
        }
    }
View Code

如果我们想要实现一个数组类型是string的,返回值也是string的方法,我们必须在新建一个类,并复制代码,然后更改数据类型,代码如下:

 public class MyGeneric_string
    {
        string[] arr;
        public void SetArr(string[] arr)
        {
            this.arr = arr;
        }

        public string GetValue(int index)
        {
            if (index<arr.Length)
            {
                return arr[index];
            }
            else
            {
                throw new Exception("索引值超出了数组的界限");
            }
        }
    }
View Code

如果再要求返回其他类型的,我们只能再新建类型并复制然后修改。。。

但是,有了泛型,一切就变的简单多了,代码如下:

 public class MyGeneric<T>
    {
        T[] arr;
        public void SetArr(T[] arr)
        {
            this.arr = arr;
        }

        public T GetValue(int index)
        {
            if (index<arr.Length)
            {
                return arr[index];
            }
            else
            {
                throw new Exception("索引值超出了数组的界限");
            }
        }
    }
View Code

我们只需在申明泛型类指明类型,其他一切都不用再修改,真是方便,快键,安全。

3.泛型类

   泛型类是某些具有相同执行指令的类的模板,它是类型的抽象或者集合。

(1)泛型类的申明

    在普通类的类名后边加尖括号,并添加类型占位符(占位符一般用T表示),如:

    

 public class MyGeneric<T>
    {
        T[] arr;
        public void SetArr(T[] arr)
        {
            this.arr = arr;
        }

        public T GetValue(int index)
        {
            if (index<arr.Length)
            {
                return arr[index];
            }
            else
            {
                throw new Exception("索引值超出了数组的界限");
            }
        }
    }
View Code

(2)泛型类的使用

        泛型类在实例化时,必须指定代替占位符的具体类,如:

static void Main(string[] args)
        {

            MyGeneric<int> mygen = new MyGeneric<int>();//用int类型代替占位符
            mygen.SetArr(new int[] {1,2,3 });
            int a=mygen.GetValue(1);

            MyGeneric<string> mygen2 = new MyGeneric<string>();//用string类型代替占位符
            mygen2.SetArr(new string[] {"1","2","3" });
            string b = mygen2.GetValue(1);
        }
View Code

4.泛型方法

    泛型方法的申明比普通方法的申明多了一对尖括号和泛型参数,例如:

   

 public void MyGenericMethod<T>(T t)

   如果有多个泛型模板,中间用“,”隔开,如:

 public T MyGenericMethod<T,R>(T t,R r)

 泛型方法的调用:我们先申明一个没有返回值的泛型方法:MyMethod

      static void MyMethod<T>(T[] arr)
        {
            foreach (T item in arr)
            {
                Console.WriteLine(item.ToString());
            }
        
        }

接着在main方法中调用:

 static void Main(string[] args)
        {
            MyMethod<string>(new string[] {"1","2","3" });//泛型类型为string
            MyMethod(new string[] { "1", "2", "3" }); //这个调用编译器自动根据参数推断类型

            MyMethod<int>(new int[] { 1, 2, 3 });
            MyMethod(new int[] {1,2,3});
        }

5.泛型委托

泛型委托的申明方式和直接申明委托的方式就多了一对尖括号,形式是这样的:

delegate R MyDelgate<T,R>(T t)

关键字说明:

delegate:申明委托的关键字

R:申明委托的返回类型

<T,R>:泛型委托的类型参数

T t :委托的形式参数

泛型委托实例:

    public delegate void MyDel<T>(T t);//没有返回类型的泛型委托
    public delegate R MyDel<T,R>(T t); //有返回类型的委托

6.泛型中的协变和逆变(待续)

7.泛型接口(待续)

原文地址:https://www.cnblogs.com/net-study/p/5067171.html