知道泛型很久了,但是一直没有好好的去研究过,最近看到很多系统的源代码中都含有泛型的编程,所以心血来潮就查阅了MSDN之后code了一些代码来理解
泛型,的确在自己写代码的过程中,能发现很多问题,这些看书都是体会不到的。
最近看到很多系统的源代码中都含有泛型的编程,所以心血来潮就查阅了MSDN之后code了一些代码来理解
泛型,的确在自己写代码的过程中,能发现很多问题,这些看书都是体会不到的。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//泛型测试2009.1.6
namespace ConsoleApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ArrayList Array = new ArrayList();
Array.Add(2);
Array.Add(8);
Array.Add("HELLO,I AM A TYPE OF STRING!");
foreach (int p in Array)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(p);
}
//这里进行onboxing取消装箱,foreach (int p in Array),其中int p的类型int是foreach执行拆箱的强制类型,
//因此如果这里是创建一个创异类集合,必定会错误,因为"HELLO,WORLD"无法转换为int
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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,这里正常输出,如果输出字符串
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class Test2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(p);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class Test3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] parm1 =
{ "I AM STRING1", "I AM STRING2", "I AM STRING3" };
Test3.Insert<string>(parm1);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void Insert<T>(T[] parms)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
List<T> ListT = new List<T>();
if (parms.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int i=0; i< parms.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ListT.Add(parms[i]);
}
}
foreach (T p in ListT)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(p);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
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,根据需要指定一个类型参数,就可以,编译器会自动检查类型是否和你提供的类型一致。
-----------------------------------------------