当前位置:网站首页>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负责处理,对一维数组赋予接口能力,非一维数组不能拥有接口能力。
当然上面这些都是对引用类型而言,值类型不会有这种动态赋予接口能力的操作。
边栏推荐
- C language: the sorting problem of circle number reporting
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- CA:用于移动端的高效坐标注意力机制 | CVPR 2021
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- Iptables foundation and Samba configuration examples
- 阿里云有奖体验:用PolarDB-X搭建一个高可用系统
- When to use pointers in go?
- 强化学习-学习笔记1 | 基础概念
- N++ is not reliable
- ArcGis利用栅格处理工具进行影像裁剪
猜你喜欢

Understand bloomfilter in one article

eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误

A data person understands and deepens the domain model
高效!用虚拟用户搭建FTP工作环境

美团·阿里关于多模态召回的应用实践

Comparative study of the gods in the twilight Era

C language function

n++也不靠谱

Meituan Ali's Application Practice on multimodal recall

Full arrangement (medium difficulty)
随机推荐
PostgreSQL 9.1 soaring Road
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误
Implementation mode and technical principle of MT4 cross platform merchandising system (API merchandising, EA merchandising, nj4x Merchandising)
Efficient! Build FTP working environment with virtual users
WPF双滑块控件以及强制捕获鼠标事件焦点
PostgreSQL 9.1 soaring Road
Besides, rsync+inotify realizes real-time backup of data
CTF竞赛题解之stm32逆向入门
Can Console. Clear be used to only clear a line instead of whole console?
SQL statement syntax error in test SQL statement deletion in eclipse linked database
Fundamentals of container technology
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
室外LED屏幕防水吗?
CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer
Is the main thread the same as the UI thread- Is main thread the same as UI thread?
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Iptables foundation and Samba configuration examples
6 分钟看完 BGP 协议。
[Android kotlin] lambda return statement and anonymous function