Ejemplo n.º 1
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.º 2
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);
        }