作者GoalBased (Artificail Intelligence)
看板C_Sharp
标题Re: [问题] IList<T>宣告
时间Mon May 30 01:39:20 2016
※ 引述《kisha024 (4545454554)》之铭言:
: 各位好
: https://msdn.microsoft.com/zh-tw/library/5y536ey6%28v=vs.110%29.aspx
: 我看 MSDN 有关 IList<T> 介面的说明是 表示可以个别由索引存取之物件的集合
: 加上底下这行程式码编译没有错
: IList<string> list = new string[] { "A", "B", "C" };
: 所以我本来以为可能是 string:IList<T> 或 Array:IList<T> 但却都不是
: 最接近的大概是 Array:IList
: 我的疑问是 上面那行程式码不会错 就是因为 Array:IList 吗?
: 如果是 那为什麽可以不用Array:IList<T> 而只要Array:IList就不会出错了呢?
: 是因为IList是interface的关系吗?
: 谢谢
查了一下MSDN,一些资料给你参考一下,顺便讲一下我查询的过程
(灰色部分),
开了vs试着对 string[] 按f12 结果只能进到string class,
所以,google:C# 阵列,得到以下资料:
https://msdn.microsoft.com/zh-tw/library/9b9dty7d.aspx
阵列类型是从抽象基底类型 Array 衍生的参考类型。 由於此类型会实作 IEnumerable
和 IEnumerable<T>,您可以在 C# 中的所有阵列上使用 foreach 反覆运算。
所以进到Array的MSDN,发现
https://msdn.microsoft.com/zh-tw/library/system.array.aspx
从.NET Framework 2.0 开始 Array 类别会实作
System.Collections.Generic.IList<T>,
System.Collections.Generic.ICollection<T>,和
System.Collections.Generic.IEnumerable<T> 泛型介面。
阵列中提供了实作在执行阶段,也因此,泛型介面不会显示在宣告语法 Array 类别。
此外,还有介面的成员只能由转型为泛型介面型别 (明确介面实作) 的阵列,
可存取的任何参考主题。
这边就知道了,阵列继承Array,Array又会实作IList<T>,所以得到答案,
但是进到Array class却没看到他说的实作阿,故把该页MSDN换回英文
https://msdn.microsoft.com/en-au/library/system.array.aspx
The implementations are provided to arrays at run time, and as a result, the
generic interfaces do not appear in the declaration syntax for the Array class
藉由看黄色那一段的原文可以知道,因为是在run time才实作,所以在Array里面
找不到这一段,结束。
黄色那段直接英翻中我看不懂,所以才去看英文的
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.226.172.184
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1464543574.A.A0A.html
1F:→ GoalBased: 顺便偷问一下,想找asp.net C# 的 startup 该去 05/30 01:48
2F:→ GoalBased: 哪边或者怎麽查比较好找呢 05/30 01:48
3F:推 kisha024: 谢谢 原本想说在VS就可以看到继承的关系 05/30 10:37
4F:→ kisha024: 没想到是run time才实作 也没想到再去查MSDN的array 05/30 10:38