Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            MyGenericArray <int> ma = new MyGenericArray <int>(5);

            for (int i = 0; i < 5; i++)
            {
                ma.SetItem(i, i + 1);
            }

            // 多重泛型
            MyGenericArray <int, string> ma2 = new MyGenericArray <int, string>();

            // 多重带限制的泛型
            MyGenericArray2 <int, string> ma3 = new MyGenericArray2 <int, string>();

            // 多重带限制的泛型
            //MyGenericArray2<string, string> m3 = new MyGenericArray2<string, string>();

            // 泛型的继承
            SonMyGenericArray <int> sma = new SonMyGenericArray <int>(3);

            // 泛型的继承也可以指定类型
            SonMyGenericArray sma2 = new SonMyGenericArray(3);

            // 泛型在方法上的实现
            // 泛型类 和 泛型方法的数据类型可以不一致
            ma.SetItem <char>(5, 'c');
            ma.SetItem <string>(5, "c");

            // 非泛型类的泛型方法
            int  a = 10; int b = 20;
            char c = 'i'; char d = 'u';

            Console.WriteLine("a:{0} b:{1}", a, b);
            Console.WriteLine("c:{0} d:{1}", c, d);
            Swap <int>(ref a, ref b);
            Swap <char>(ref c, ref d);
            Console.WriteLine("a:{0} b:{1}", a, b);
            Console.WriteLine("c:{0} d:{1}", c, d);

            // 泛型在委托中的应用
            NumberChanger <int> n1 = new NumberChanger <int>(AddNum);
            NumberChanger <int> n2 = new NumberChanger <int>(MultNum);

            n1(25);
            n2(5);

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //泛型在Class上的实现
            MyGenericArray <int, string> intArray = new MyGenericArray <int, string>(5);

            for (int index = 0; index < 5; index++)
            {
                intArray.SetItem(index, index * 5);
            }
            for (int index = 0; index < 5; index++)
            {
                Console.Write(intArray.GetItem(index) + " ");
            }
            Console.WriteLine();

            MyGenericArray <char, string> charArray = new MyGenericArray <char, string>(5);

            for (int index = 0; index < 5; index++)
            {
                charArray.SetItem(index, (char)(index + 97));
            }
            for (int index = 0; index < 5; index++)
            {
                Console.Write(charArray.GetItem(index) + " ");
            }
            Console.WriteLine();

            Console.WriteLine("------------------------");//泛型在方法上的实现
            ParentGeneric <int, string> gm = new ParentGeneric <int, string>();

            gm.GenericMethod <string>("Hello Generic Method");
            gm.GenericMethod <int>(100);

            string s1 = "hello";
            string s2 = "zilong";

            Swap <string>(ref s1, ref s2);
            Console.WriteLine("item1 = {0}, item2 = {1}", s1, s2);

            int i1 = 100;
            int i2 = 99;

            Swap <int>(ref i1, ref i2);
            Console.WriteLine("item1 = {0}, item2 = {1}", i1, i2);

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        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.Write(intArray.GetItem(c) + " ");
            }
            Console.WriteLine();

            intArray.GenericMethod <string>("Hello Generic!");
            intArray.GenericMethod <int>(100);

            MyGenericArray <char> charArray = new MyGenericArray <char>(5);

            for (int c = 0; c < 5; c++)
            {
                charArray.SetItem(c, (char)(c + 97));
            }
            for (int c = 0; c < 5; c++)
            {
                Console.Write(charArray.GetItem(c) + " ");
            }
            Console.WriteLine();

            int  m, n;
            char x, y;

            m = 10;
            n = 20;
            x = 'I';
            y = 'V';
            Console.WriteLine("m:{0};n:{1}", m, n);
            Console.WriteLine("x:{0};y:{1}", x, y);

            Swap <int>(ref m, ref n);
            Swap <char>(ref x, ref y);

            Console.WriteLine("m:{0};n:{1}", m, n);
            Console.WriteLine("x:{0};y:{1}", x, y);
        }
Ejemplo n.º 4
0
        public 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) + " ");
            }
            Console.WriteLine();


            //声明一个字符数组
            MyGenericArray <char> charArray = new MyGenericArray <char>(10);

            //设置值
            for (int i = 0; i < 10; i++)
            {
                charArray.setItem(i, (char)(i + 97));
            }
            //获取值
            for (int k = 0; k < 10; k++)
            {
                Console.WriteLine(charArray.getItem(k) + " ");
            }

            Console.WriteLine();
            Console.ReadKey();



            int  a, b;
            char e, f;

            a = 10;
            b = 5;
            e = 'f';
            f = 'g';


            // 在交换之前显示值
            Console.WriteLine("Int values before calling swap:");
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine("Char values before calling swap:");
            Console.WriteLine("c = {0}, d = {1}", e, f);
            // 调用 swap
            Program.Swap <int> (ref a, ref b);
            Program.Swap <char> (ref e, ref f);

            // 在交换之后显示值
            Console.WriteLine("Int values after calling swap:");
            Console.WriteLine("a = {0}, b = {1}", a, b);
            Console.WriteLine("Char values after calling swap:");
            Console.WriteLine("c = {0}, d = {1}", e, f);
            Console.ReadKey();


            NumberChanger <int> cn1 = new NumberChanger <int>(AddNum);
            NumberChanger <int> cn2 = new NumberChanger <int>(MultNum);

            cn1(25);
            cn2(5);
            Console.WriteLine("Value of Num: {0}", getNum());
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            //instanciating class or declaring the array
            //MyGenericArray obj = new MyGenericArray(5);
            ////setting the values
            //for(int i=0;i<5;i++)
            //{
            //    obj.setItem(i, i * 5);
            //}

            ////retrieving the values
            //for (int i = 0; i < 5; i++)
            //{
            //    Console.Write(obj.getItem(i) + " ");
            //}

            //Generic class
            MyGenericArray <int> objIntArray = new MyGenericArray <int>(5);

            //setting the values
            for (int i = 0; i < 5; i++)
            {
                objIntArray.setItem(i, i * 5);
            }

            //retrieving the values
            for (int i = 0; i < 5; i++)
            {
                Console.Write(objIntArray.getItem(i) + " ");
            }

            Console.WriteLine();
            Console.WriteLine("Entering characters");
            //declaring a character array
            MyGenericArray <char> objCharArray = new MyGenericArray <char>(5);

            //setting the values
            for (int i = 0; i < 5; i++)
            {
                objCharArray.setItem(i, (char)(i + 97));
            }

            //retrieving the values
            for (int i = 0; i < 5; i++)
            {
                Console.Write(objCharArray.getItem(i) + " ");
            }
            Console.WriteLine();
            //non-Generic method
            GenericMethod objMethod = new GenericMethod();
            int           a         = 10;
            int           b         = 20;
            char          c         = 'S';
            char          d         = 'M';

            Console.WriteLine("Values before swapping");
            Console.WriteLine("a={0},b={1}", a, b);
            Console.WriteLine("c={0},d={1}", c, d);

            objMethod.swapInt(ref a, ref b);
            objMethod.swapChar(ref c, ref d);

            Console.WriteLine("Values after swapping");
            Console.WriteLine("a={0},b={1}", a, b);
            Console.WriteLine("c={0},d={1}", c, d);


            //generic method
            Console.WriteLine("Values before swapping in Genreric");
            Console.WriteLine("a={0},b={1}", a, b);
            Console.WriteLine("c={0},d={1}", c, d);

            objMethod.swapGeneric <int>(ref a, ref b);
            objMethod.swapGeneric <char>(ref c, ref d);

            Console.WriteLine("Values after swapping in Generic");
            Console.WriteLine("a={0},b={1}", a, b);
            Console.WriteLine("c={0},d={1}", c, d);

            Console.WriteLine();
            Console.WriteLine("Generic with 2 methods");
            objMethod.swapGeneric1 <int, string>(5, "Sachin");
            objMethod.swapGeneric1 <int, int>(5, 6);
            Console.WriteLine();



            Console.ReadLine();
        }