当前位置:网站首页>c#数组补充
c#数组补充
2022-07-04 12:42:00 【华为云】
c#数组的几个简单的补充
数组是引用类型,不论其元素是值类型还是引用类型。
c#中数组对象实现的各个接口:
// 定义了浅拷贝的能力,因此数组可以进行浅拷贝System.ICloneable// ICollection接口的子代,使用接口编程减轻耦合,该接口只定义了一些方法,有索引器,查询元素位置,插入数据,移除索引位置数据这个方法,简单的可以当作存放数据的容器,不能像List那样具有很多数据处理的方法。System.Collections.IList// 集合的父基类,定义了返回元素数量,复制等一些集合类型最基本的方法,同时提供了可以同步访问ICollection对象元素的方法,数组具有这些能力System.Collections.ICollection// 定义迭代遍历的能力,数组可以使用foreach循环System.Collections.IEnumerable// .net4 新增接口,定义了排序的能力,数据可以进行排序因此而来System.Collections.IStructuralComparable// .net4 新增接口,定义比较能力,数组可以比较数组是否具有相同的内容如果元素数量不同直接返回不相等,同时具有几个元素就会执行几次比较System.Collections.IStructuralEquatable// 也提供了索引器,删除插入查询数据几个方法,但是是泛型数据/* 特殊的,所有的数组默认继承自Array抽象类,该类实现了如上的接口,但在查看源码不能看到数组实现如下接口,这些接口实在CLR动态为一维数组实现如下的接口 */System.Collections.Generic.IList <T>// 提供泛型的能力,也有基本的增删改查的功能System.Collections.Generic.ICollection <T>// 一个类型可以使用linq迭代,必须实现改接口,因此数组可以使用linq查询。System.Collections.Generic.IEnumerable <T>// 只读list,通过索引访问该只读list的元素System.Collections.Generic.IReadOnlyList <T>// 只读集合,通过索引访问该只读集合的元素System.Collections.Generic.IReadOnlyCollection <T>
代码查看:
foreach (var type in (new int[0,0]).GetType().GetInterfaces()) Console.WriteLine(type);
通过执行该代码,当创建的数组是一维数组时,该数组对象会实现泛型接口,可以拥有linq查询的能力,但当不是一维数组时,此时的数据就不具有linq的能力,此时的数组对象不实现泛型接口。
这些动作是由CLR (公共语言运行时)执行的,内部原理并不清楚,但一般而言linq查询是对集合进行操作,数组属于集合,但由于存在多维数组,数组不能直接实现IEnumerable泛型接口获取linq查询的能力,而是在托管代码运行时由CLR负责处理,对一维数组赋予接口能力,非一维数组不能拥有接口能力。
当然上面这些都是对引用类型而言,值类型不会有这种动态赋予接口能力的操作。
边栏推荐
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- Reptile exercises (I)
- Using nsproxy to forward messages
- 「小技巧」给Seurat对象瘦瘦身
- runc hang 导致 Kubernetes 节点 NotReady
- 8 expansion sub packages! Recbole launches 2.0!
- Is there an elegant way to remove nulls while transforming a Collection using Guava?
- Rsyslog配置及使用教程
- Argminer: a pytorch package for processing, enhancing, training, and reasoning argument mining datasets
- Transformer principle and code elaboration (pytorch)
猜你喜欢
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
2022年中国移动阅读市场年度综合分析
A treasure open source software, cross platform terminal artifact tabby
Runc hang causes the kubernetes node notready
Master the use of auto analyze in data warehouse
Play Sanzi chess easily
Transformer principle and code elaboration (pytorch)
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
光环效应——谁说头上有光的就算英雄
随机推荐
Sort merge sort
Play Sanzi chess easily
AI 绘画极简教程
Runc hang causes the kubernetes node notready
After installing vscode, the program runs (an include error is detected, please update the includepath, which has been solved for this translation unit (waveform curve is disabled) and (the source fil
七、软件包管理
PostgreSQL 9.1 soaring Road
众昂矿业:为保障萤石足量供应,开源节流势在必行
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
ArcGIS uses grid processing tools for image clipping
n++也不靠谱
AbstractDispatcherServletInitializer 的实现类为什么可以在初始化Web容器的时候被调用
SQL statement syntax error in test SQL statement deletion in eclipse linked database
A taste of node JS (V), detailed explanation of express module
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
How real-time cloud interaction helps the development of education industry
MDK在头文件中使用预编译器时,#ifdef 无效的问题
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
Comparative study of the gods in the twilight Era
Configure WebDAV server on Apache