本帖最后由 faunus 于 2014-2-24 11:29 编辑
首先看一下Array的定义:
- public abstract class Array : ICloneable, IList, ICollection, IEnumerable
复制代码
并没有实现泛型接口
实际Array是支持这些泛型接口的:
- using System;
- using System.Collections.Generic;
- //using System.SZArrayHelper//无法调用
- class MyClass
- {
- static void Main(string[] args)
- {
- int[] intArray = { 1, 2, 3, };
- //类型测试
- Console.WriteLine(intArray.GetType().ToString());//System.Int32[]
- Console.WriteLine(intArray is IEnumerable<int>);//True
- Console.WriteLine(intArray is ICollection<int>);//True
- Console.WriteLine(intArray is IList<int>);//True
-
- //转换到接口
- IEnumerable<int> ieT = intArray;
- ICollection<int> icT = intArray;
- IList<int> icL = intArray;
- //IList接口部分//接口的定义中有
- //intArray.Insert();//并没有实现
- //intArray.Remove();//并没有实现
- //intArray.RemoveAt();//并没有实现
- //转化为接口后,就有定义
- icL.Insert(0, 4);
- //未处理的异常: System.NotSupportedException: 集合的大小是固定的。
- //在 System.SZArrayHelper.Insert[T](Int32 index, T value)
- icL.Remove(1);
- //未处理的异常: System.NotSupportedException: 集合的大小是固定的。
- //在 System.SZArrayHelper.Remove[T](T value)
- icL.RemoveAt(1);
- //未处理的异常: System.NotSupportedException: 集合的大小是固定的。
- //在 System.SZArrayHelper.RemoveAt[T](Int32 index)
- }
- }
复制代码
通过实例测试,Array又确实是实现了这些接口的。
同样包括前面的老问题:
Array实现了:IList(包含Insert、Remove、RemoveAt)
Array没实现:Insert、Remove、RemoveAt
在转换为接口后会抛出:System.NotSupportedException异常。
感谢rightyeah朋友的工作,我引用如下:
我查过,Array类实现了IList所有的方法,
可以用ildasm查看mscorlib.dll,Array实现了
System.Collections.IList接口的 Add,Clear,Contains,IndexOf,Insert ,Remove ,RemoveAt ,get_Item,set_Item方法
注意:是ildasm中查看到的结果,不是对象浏览器中的结果。
再看一下MSDN中的重要声明:
http://msdn.microsoft.com/zh-cn/library/system.array.aspx
在 .NET Framework 2.0 版中,Array 类实现 System.Collections.Generic.IList <(Of <(T>)>)、 System.Collections.Generic.ICollection <(Of <(T>)>) 和 System.Collections.Generic.IEnumerable <(Of <(T>)>) 泛型接口。由于实现是在运行时提供给数组的,因而对于文档生成工具不可见。因此,泛型接口不会出现在 Array 类的声明语法中,也不会有关于只能通过将数组强制转换为泛型接口类型(显式接口实现)才可访问的接口成员的参考主题。将某一数组强制转换为这三种接口之一 时需要注意的关键一点是,添加、插入或移除元素的成员会引发 NotSupportedException。
同时我们还意外发现:
.net还藏了不少的私货
System.SZArrayHelper你用不了,
但MS可以,呵呵。
非泛型数且和泛型数组哪个更快点,
这个不想深入,只是引用一些网友现成的结果:
同一个数组泛型和非泛型时的Enumerator不同.
System.Array+SZArrayEnumerator
System.SZArrayHelper+SZGenericArrayEnumerator`1[System.Object]
用Reflector看他们的实现,大多数代码一样,唯一的区别是,
System.Array+SZArrayEnumerator.Current属性的实现:
return this._array.GetValue(this._index);
而System.SZArrayHelper+SZGenericArrayEnumerator`1[System.Object].Current属性的实现:
return this._array[this._index];
注意,前一个_array是Array类型的,后一个是object[]类型的.
也就是说,
[1]非泛型时,getvalue需要执行Array.GetValue()也就是下面这段额外的代码: - <font color="#333333"><font face="Arial"><font style="font-size: 14.399999618530273px">public unsafe object GetValue(int index) { if (this.Rank != 1) { throw new ArgumentException(Environment.GetResourceString("Arg_Need1DArray")); } TypedReference reference = new TypedReference(); this.InternalGetReference((void*)&reference, 1, &index); return TypedReference.InternalToObject((void*)&reference); }</font></font></font>
复制代码 [2]而泛型直接一个索引就返回值了.
|