1. 数组隐式继承 System.Array,所以数组是引用类型。变量包含的是对数组的引用,而不是包含数据本身的元素。

2. 数组协变性。将数组从一种类型转换为另一种类型。

string[] sa = new string[100];
object[] oa = sa;
oa[5] = "Jeff"; // 性能损失:CLR检查oa的元素类型是不是String;检查通过
oa[3] = 5; // 性能损失:CLR检查oa的元素类型是不是Int32;发现有错,抛异常

System.Buffer的BlockCopy方法,只支持基元类型,不具有转型能力。

System.Array的ConstrainedCopy方法,要么完美复制,要么抛出异常,不会破坏目标数组中的数据,不执行装箱、拆箱和向下类型转换。

3. 所有数组都隐式实现 IEnumerable,ICollection,IList

4. 创建下限非0的数组

int[] lowerBounds = { 2015, 1 };
int[] lengths = { 5, 4 };
decimal[,] quarterlyRevenue = (decimal[,])Array.CreateInstance(typeof(decimal), lengths, lowerBounds);
Console.WriteLine("{0,4} {1,9} {2,9} {3,9} {4,9}", "year", "Q1", "Q2", "Q3", "Q4");
int fristYear = quarterlyRevenue.GetLowerBound(0); // 2015
int lastYear = quarterlyRevenue.GetUpperBound(0); // 2019
int fristQuarter = quarterlyRevenue.GetLowerBound(1); // 1
int lastQuarter = quarterlyRevenue.GetUpperBound(1); // 4
for (int year = fristYear; year <= lastYear; year++)
{
Console.Write(year + " ");
for (int quarter = fristQuarter; quarter < lastQuarter; quarter++)
{
Console.Write("{0,9:C} ", quarterlyRevenue[year, quarter]);
}
Console.WriteLine();
}

5. 数组只有两种,下限为0的一维数组(System.String[])和下限未知的多维数组(System.String[,])。1维1基数组(System.String[*])C#不允许使用。访问一维0基数组比访问非0基多维数组要快,因为在for循环中JIT编译器会生成代码来检查是否 0 >= GetLowerBound(0) && (length-1) <= GetUpperBound(0),一维0基数组只会在for循环前检查1次,非0基多维数组则每次都会检查。

private const int c_numElements = 10000;
public static void Main()
{
int[,] a2Dim = new int[c_numElements, c_numElements];
int[][] aJagged = new int[c_numElements][];
for (int x = 0; x < c_numElements; x++)
{
aJagged[x] = new int[c_numElements];
} Safe2DimArrayAccess(a2Dim); SafeJaggedArrayAccess(aJagged); Unsafe2DimArrayAccess(a2Dim);
}
private static int Safe2DimArrayAccess(int[,] a)
{
int sum = 0;
for (int x = 0; x < c_numElements; x++)
{
for (int y = 0; y < c_numElements; y++)
{
sum += a[x, y];
}
}
return sum;
}
private static int SafeJaggedArrayAccess(int[][] a)
{
int sum = 0;
for (int x = 0; x < c_numElements; x++)
{
for (int y = 0; y < c_numElements; y++)
{
sum += a[x][y];
}
}
return sum;
}
private static unsafe int Unsafe2DimArrayAccess(int[,] a)
{
int sum = 0;
fixed (Int32* pi = a)
{
for (int x = 0; x < c_numElements; x++)
{
int baseOfDim = x * c_numElements;
for (int y = 0; y < c_numElements; y++)
{
sum += pi[baseOfDim + y];
}
}
}
return sum;
}

6. 栈上分配数组

public static void Main()
{
StackallocDemo();
InlineArrayDemo();
}
private static void StackallocDemo()
{
unsafe
{
const int width = 20;
Char* pc = stackalloc char[width]; // 在栈上分配数组
string s = "Jeffrey Richter";
for (int index = 0; index < width; index++)
{
pc[width - index - 1] = (index < s.Length) ? s[index] : '.';
}
Console.WriteLine(new string(pc, 0, width));
}
} private static void InlineArrayDemo()
{
unsafe
{
CharArray ca;
Int32 widthInByBytes = sizeof(CharArray);
Int32 width = widthInByBytes / 2; string s = "Jeffrey Richter";
for (int index = 0; index < width; index++)
{
ca.Characters[width - index - 1] = (index < s.Length) ? s[index] : '.';
}
Console.WriteLine(new string(ca.Characters, 0 ,width));
}
} internal unsafe struct CharArray
{
public fixed Char Characters[20];
}

CLR via C# 笔记 -- 数组(16)的更多相关文章

  1. <NET CLR via c# 第4版>笔记 第16章 数组

    //创建一个一维数组 int[] myIntegers; //声明一个数组引用 myIntegers = new int[100]; //创建含有100个int的数组 //创建一个二维数组 doubl ...

  2. Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 今天的内容是Scaffold脚手架.AppBar组件.BottomNavigationBa ...

  3. javaSE学习笔记(16)---网络编程

    javaSE学习笔记(16)---网络编程 基本概念 如今,计算机已经成为人们学习.工作.生活必不可少的工具.我们利用计算机可以和亲朋好友网上聊天,也可以玩网游.发邮件等等,这些功能实现都离不开计算机 ...

  4. java JDK8 学习笔记——第16章 整合数据库

    第十六章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作,称为JDBC驱动程 ...

  5. RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群

    1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...

  6. 《C#本质论》读书笔记(16)构建自定义集合

    16.1 更多集合接口 集合类(这里指IEnumerable层次结构)实现的接口层次结构 16.1.1 IList<T>与IDictionary<TKey,TValue> 字典 ...

  7. 读CLR via C#笔记

    1.is 和 as 的区别 public class Employee { } a): object obj = new Employee(); if (obj is Employee) { Empl ...

  8. Java菜鸟学习笔记--数组篇(三):二维数组

    定义 //1.二维数组的定义 //2.二维数组的内存空间 //3.不规则数组 package me.array; public class Array2Demo{ public static void ...

  9. php 学习笔记 数组3

    15.使用数组 1).并集(union)  array_merge(array1,array2,array3..) 函数把两个或多个数组合并为一个数组,后面覆盖前面 2). 交集(intersecti ...

  10. java笔记 -- 数组

    概念: 数组是一种数据结构, 用来存储同一类型值的集合. 通过一个整型的下标可以访问数组中的每一个值. 声明: int[] a(推荐,将类型int[](整形数组)和变量名分开了) 或者int a[] ...

随机推荐

  1. 深入理解 C++ 中的多态与文件操作

    C++ 多态 多态(Polymorphism)是面向对象编程(OOP)的核心概念之一,它允许对象在相同操作下表现出不同的行为.在 C++ 中,多态通常通过继承和虚函数来实现. 理解多态 想象一个场景, ...

  2. Solution Set - 矩阵加速

    A[HDU2604]求不含子串010和000的,长为\(n\)的01序列数. B[HDU6470]数列\(\{a_n\}:a_1=1,a_2=2,a_n=a_{n-1}+2a_{n-2}+n^3\), ...

  3. ruby执行周期性任务 whenever

    ruby执行周期性任务 下面看看怎么将任务写入cron服务. $ whenever #不带参数的whenever会显示转换程cron任务的代码,不写入cron任务表 $ whenever -w #写入 ...

  4. linux开发vue项目,不能热更新?

    只需要运行下面的命令即可: echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo ...

  5. 简易版跳板机-teleport使用

    目录 1 环境搭建 2 teleport工具搭建 3 teleport使用示例 3.1 资产管理-添加主机 3.2 资产管理-添加账号 3.3 创建用户 3.4 运维授权 3.5 安装客户端助手 3. ...

  6. uiautomator2使用方法

    一.设备连接 1.usb单设备连接 d = u2.connect() 2.usb多设备连接 d = u2.connect("90bf8faf") # 多台设备填写device即可 ...

  7. 智慧城市three.js数字孪生三维展示城市建筑轮廓数据获取方法分析和AI算法方案剖析

    一.目前世面上有的2种免费方案是 1.OpenStreetMap开源网站下载, 2.blender的gis插件获取城市建筑轮廓. 缺点是:只有重点城市和省会城市,一些地级市或者县级市基本没有数据. 二 ...

  8. cesium基础知识汇总PPT版

    以上教程来自火星科技,原视频教程地址如下: https://ke.qq.com/course/468292/3985600802137412#term_id=100560563

  9. VForm

    VForm是一款基于Vue 2/Vue 3的低代码表单,支持Element UI.iView两种UI库,定位为前端开发人员提供快速搭建表单.实现表单交互和数据收集的功能. VForm全称为Varian ...

  10. Spring源码阅读 ------------------- SpringFrameWork 5.2 +IDEA 部署其他坑位问题(二)

    一.地址下jar包无法下载问题   报错信息:      Could not get resource 'https://plugins.gradle.org/m2/org/asciidoctor/o ...