日期:2014-05-18  浏览次数:20416 次

看了一上午,做了一个Test,理解泛型。。。。。。。。。散分了
C# code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

//泛型测试2009.1.6
namespace ConsoleApplication1
{
    class Test
    {
        static void Main(string[] args)
        {
            ArrayList Array = new ArrayList();
            Array.Add(2);
            Array.Add(8);
            Array.Add("HELLO,I AM A TYPE OF STRING!");
            foreach (int p in Array)
            {
                Console.WriteLine(p);
            }
            //这里进行onboxing取消装箱,foreach (int p in Array),其中int p的类型int是foreach执行拆箱的强制类型,
            //因此如果这里是创建一个创异类集合,必定会错误,因为"HELLO,WORLD"无法转换为int

            Console.WriteLine(Int32.Parse(Array[0].ToString()));
            Console.WriteLine(Int32.Parse(Array[1].ToString()));
            //string aa = Array[2];//为了证明加入集合的字符串已经不再是字符串,这里是装箱过的类型object,所以编译错误,无法将object隐式转换成string,
            Console.WriteLine(Array[2]);//WriteLine接受参数本身就是object,这里正常输出,如果输出字符串

             
        }
    }
}


 class Test2
    {
        static void Main(string[] args)
        {
            List<int> List3 = new List<int>();
            List3.Add(0);
            List3.Add(1);
            List3.Add(2);
            List3.Add("i am not here?");//编译器不能通过。
            int the1 = List3[0];//呵呵,和上面相同的测试方式,这里说明加入的集合数据没有进行装箱操作,集合元素本身的确是一个int类型
            foreach (int p in List3)
             {
                 Console.WriteLine(p);
             }
        }
 
    }

  class Test3
    {
        static void Main(string[] args)
        {
            string[] parm1 = { "I AM STRING1", "I AM STRING2", "I AM STRING3" };
            Test3.Insert<string>(parm1);
        }

       public static void Insert<T>(T[] parms)
        {
            List<T> ListT = new List<T>();
            if (parms.Length > 0)
            {
                for (int i=0; i< parms.Length; i++)
                {
                    ListT.Add(parms[i]);
                }
            }
            foreach (T p in ListT)
            {
                Console.WriteLine(p);
            }
                  


        }
    }



Test1为什么要泛型?
在foreach检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能,而且对于创建异类集合的时候。
即向集合加入不同类型数据的时候,如上, foreach (int p in Array)就不能迭代所有内容,遇见字符串类型错误,而且C#编译器在编译的时不出错,在运行时才显示此错误,

Test2代码使用了泛型?
代码说明了加入集合的过程没有在进行装箱向上转换为object的操作了,而且编译器能够通过安全检查,只要是输入非整形的就会报错的,
这里编译器会同时候报两次错误,
错误 1 与“System.Collections.Generic.List<int>.Add(int)”最匹配的重载方法具有一些无效参数 C:\Documents and Settings\Administrator\桌面

\ConsoleApplication1\ConsoleApplication1\Program.cs 41 13 ConsoleApplication1
错误 2 参数“1”: 无法从“string”转换为“int” C:\Documents and Settings\Administrator\桌面\ConsoleApplication1\ConsoleApplication1

\Program.cs 41 23 ConsoleApplication1;

Test3如何用泛型?
第三个示例是简单的泛型应用,插入几个方法Insert,根据需要指定一个类型参数,就可以,编译器会自动检查类型是否和你提供的类型一致。

本人学习中,欢迎指正,谢谢大家


------解决方案--------------------
up~
------解决方案--------------------
收藏
------解决方案--------------------
来看你头像...
------解决方案--------------------
mark
------解决方案--------------------
学习...
------解决方案--------------------
顶钻研贴~
------解决方案--------------------
来乐 接分了
------解决方案--------------------
学习
------解决方案--------------------