当前位置:网站首页>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负责处理,对一维数组赋予接口能力,非一维数组不能拥有接口能力。
当然上面这些都是对引用类型而言,值类型不会有这种动态赋予接口能力的操作。
边栏推荐
- Besides, rsync+inotify realizes real-time backup of data
- 爬虫练习题(一)
- Apache server access log access Log settings
- 【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
- Will the concept of "being integrated" become a new inflection point of the information and innovation industry?
- 数据库锁表?别慌,本文教你如何解决
- 模块化笔记软件综合评测:Craft、Notion、FlowUs
- Etcd 存储,Watch 以及过期机制
- C language array
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
猜你喜欢
【云原生 | Kubernetes篇】深入了解Ingress(十二)
Annual comprehensive analysis of China's mobile reading market in 2022
美团·阿里关于多模态召回的应用实践
ISO 27001 Information Security Management System Certification
实时云交互如何助力教育行业发展
Play Sanzi chess easily
Reinforcement learning - learning notes 1 | basic concepts
How real-time cloud interaction helps the development of education industry
DC-5 target
Will the concept of "being integrated" become a new inflection point of the information and innovation industry?
随机推荐
Can Console. Clear be used to only clear a line instead of whole console?
Simple understanding of binary search
Comprehensive evaluation of modular note taking software: craft, notation, flowus
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
美团·阿里关于多模态召回的应用实践
Agile development / agile testing experience
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
【Android Kotlin】lambda的返回语句和匿名函数
高效!用虚拟用户搭建FTP工作环境
Master the use of auto analyze in data warehouse
使用宝塔部署halo博客
DGraph: 大规模动态图数据集
Runc hang causes the kubernetes node notready
Full arrangement (medium difficulty)
读《认知觉醒》
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
WPF双滑块控件以及强制捕获鼠标事件焦点
N++ is not reliable
ArcGIS uses grid processing tools for image clipping
Deploy halo blog with pagoda