C#学习记录(26)泛型(1)

    首先介绍泛型的概念,先学习泛型的抽象术语,因为学习泛型的概念对高效使用它是至关重要的。

    日常生活中经常用到一些文档模板,它的格式已经写好,我们只需要填入姓名、日期等信息即可使用。泛型是C#一个非常重要的特性,通过这个技术,我们可以定义像文档模板一样的类模板。

        static void Main(string[] args)
        {
            Queue q = new Queue();  ///Queue集合类,队列。q是object
            q.Enqueue(10);
            q.Enqueue("23");
            q.Enqueue(3.13);
            foreach (int cc in q)    ///拆箱过程
                Console.WriteLine(cc);

        }

    ///以上程序编译通过,但是运行的时候会报错,因为foreach循环的是整型,可是集合中有三种数据类型。

    为什么用泛型?

    一般集合操作的缺点如下:

    (1)在装箱拆箱过程中会损失性能;

    (2)类型安全检查容易遗漏

    (3)显式转换会降低程序的可读性。

    除了定义泛型类,我们还可以定义泛型方法,泛型接口,泛型结构,泛型委托等。

    二.使用泛型

    1.定义泛型类

        class MyQueue<T>    ///泛型类
    {
        T[] items;  ///泛数组
        private int head;
        private int tail;
        public MyQueue(int length)  ///构造函数
        {
            head = 0;
            tail = 0;
            items = new T[length];  //定义数组的长度
        }


        //进队函数
        public void Enqueue(T item)
        {
            if ((tail + 1) % items.Length != head ) ///
            {
                tail = (tail + 1) % items.Length;
                items[tail] = item;
            }
            else
            {
                throw new Exception("Queue is not full.");
            }

        }

        //出队函数
        public T Dequeue()
        {
            if (head != tail) ///
            {
                head = (head + 1) % items.Length;
                return items[head];
            }
            else
            {
                throw new Exception("Queue is not full.");
            }
        }

    }

    
       static void Main()

       {

             MyQueue<int> q1 = new MyQueue<int>(20); ////此时定义的T就是整型

            q1.Enqueue(1);
            q1.Enqueue(2);      ///不用拆箱装箱
            //q1.Enqueue("3");  ////编译格式检查错误被直接扼杀在摇篮中。
            q1.Enqueue(3);      ///程序可读性更强
            q1.Enqueue(4);
            q1.Enqueue(5);
            q1.Enqueue(6);
            for (int i = 0; i < 6; i++)
                Console.WriteLine(q1.Dequeue());


            MyQueue<string> q2 = new MyQueue<string>(20); ////此时定义的T就是整型
            q2.Enqueue("a");
            q2.Enqueue("sdsdb");      ///不用拆箱装箱
            q2.Enqueue("c");      ///程序可读性更强
            q2.Enqueue("d");
            q2.Enqueue("e");
            q2.Enqueue("f");
            for (int i = 0; i < 6; i++)
                Console.WriteLine(q2.Dequeue());
        }


        2.定义泛型方法:

            ////定义泛型方法
            int x = 60, y = 90;
            Swap<int>(ref x, ref y);    ///T代表的就是int类型
            Console.WriteLine("x={0},y={1}",x,y);

        public static void Swap<T>(ref T x, ref T y)
        {
            T t;
            t = x;
            x = y;
            y = t;
        }

     

    3.定义泛型接口:定义泛型接口与定义泛型类所用的技术相同

    interface MyFarmingInterface<T>
    where T : Animal
    {
            bool AttemptToBreed(T animal1, T animal2);
            T OldestInHerd { get; }

    }

    

    4.定义泛型委托

    public delegate int MyDelegate(int op1, int op2);

    要定义泛型委托,只需声明和使用一个或多个泛型类型参数,例如:

    public     delegate     T1     MyDelegate<T1, T2>(T2 op1, T2 op2)     where     T1: T2;

    

猜你喜欢

转载自blog.csdn.net/shenseyoulan/article/details/80907776