• 泛型


    泛型在Class上的实现

    class Program
        {
            //提高数据重用性 int,string,char,...
            //类型安全
            //性能较好
            static void Main(string[] args)
            {
                MyGenericArray<int> intArray = new MyGenericArray<int>(5);
                for (int c = 0; c < 5; c++)
                {
                    intArray.SetItem(c, c * 5);
                }
                for (int c = 0; c < 5; c++)
                {
                    Console.WriteLine(intArray.GetItem(c));
                }
                MyGenericArray<char> CharArray = new MyGenericArray<char>(5);
                for (int c = 0; c < 5; c++)
                {
                    intArray.SetItem(c, c + 97);
                }
                for (int c = 0; c < 5; c++)
                {
                    Console.WriteLine((char)intArray.GetItem(c));
                }
                Console.ReadLine();
            }
        }
        public class MyGenericArray<T>
        {
            private T[] array;
            public MyGenericArray(int size)
            {
                array = new T[size];
            }
            public T GetItem(int index)
            {
                return array[index];
            }
            public void SetItem(int index, T value)
            {
                array[index] = value;
            }
        }

     进一步探讨

    class Program
        {
            //提高数据重用性 int,string,char,...
            //类型安全
            //性能较好
            static void Main(string[] args)
            {
                MyGenericArray<int, char> intArray = new MyGenericArray<int, char>(5);
                for (int c = 0; c < 5; c++)
                {
                    intArray.SetItem(c, c * 5);
                }
                for (int c = 0; c < 5; c++)
                {
                    Console.WriteLine(intArray.GetItem(c));
                }
                MyGenericArray<char, string> CharArray = new MyGenericArray<char, string>(5);
                for (int c = 0; c < 5; c++)
                {
                    intArray.SetItem(c, c + 97);
                }
                for (int c = 0; c < 5; c++)
                {
                    Console.WriteLine((char)intArray.GetItem(c));
                }
                Console.ReadLine();
            }
        }
        public class MyGenericArray<T1, T2> where T1 : struct
        {
            private T1[] array;
            public MyGenericArray(int size)
            {
                array = new T1[size];
            }
            public T1 GetItem(int index)
            {
                return array[index];
            }
            public void SetItem(int index, T1 value)
            {
                array[index] = value;
            }
        }

    泛型的继承

    class Program
        {
            //提高数据重用性 int,string,char,...
            //类型安全
            //性能较好
            static void Main(string[] args)
            {
                MyGenericArray<int> intArray = new MyGenericArray<int>(5);
                for (int c = 0; c < 5; c++)
                {
                    intArray.SetItem(c, c * 5);
                }
                for (int c = 0; c < 5; c++)
                {
                    Console.WriteLine(intArray.GetItem(c));
                }
                MyGenericArray<char> CharArray = new MyGenericArray<char>(5);
                for (int c = 0; c < 5; c++)
                {
                    intArray.SetItem(c, c + 97);
                }
                for (int c = 0; c < 5; c++)
                {
                    Console.WriteLine((char)intArray.GetItem(c));
                }
                Console.ReadLine();
            }
        }
        public class MyGenericArray<T> where T : struct
        {
            private T[] array;
            public MyGenericArray(int size)
            {
                array = new T[size];
            }
            public T GetItem(int index)
            {
                return array[index];
            }
            public void SetItem(int index, T value)
            {
                array[index] = value;
            }
        }
        public class SubClass : MyGenericArray<int>
        {
            public SubClass(int i) : base(i) { }
        }
        public class SubGeneric<T> : MyGenericArray<int> where T : struct
        {
            public SubGeneric(int i) : base(i) { }
        }

     泛型方法上的实现

    方法:

    private static void myswap<T>(ref T a, ref T b)
            {
                T temp;
                temp = a;
                a = b;
                b = temp;
            }

    调用:

    int aa = 666, bb = 777;
                myswap<int>(ref aa, ref bb);
                Console.WriteLine("a:{0},b:{1}", aa, bb);

     泛型委托上的实现

            delegate T NumberChanger<T>(T n);
            static int num = 10;
            public static int AddNum(int p)
            {
                return num += p;
            }
            public static int MultiNum(int p)
            {
                return num *= p;
            }
            static void Main(string[] args)
            {
                NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
                NumberChanger<int> nc2 = new NumberChanger<int>(MultiNum);
                Console.WriteLine("10+5:{0},*10:{1}", AddNum(5), MultiNum(10));

     

  • 相关阅读:
    JS如何判断是否已经引入某个css或是js?
    FILTER:progid:DXImageTransform.Microsoft.Gradient使用
    根据ip查询ip归属地
    java判断是否是合法IP
    jQuery下实现等待指定元素加载完毕(可改成纯js版)
    jsp 循环数字
    jsp获取当前日期
    MyBatis In的使用
    给半径求圆面积
    实现一个数值加法函数,返回两个数字的和。要求能进行参数验证,若参数不是数字或为空则抛出异常。
  • 原文地址:https://www.cnblogs.com/handsomer/p/4550893.html
Copyright © 2020-2023  润新知