CLR via C# 笔记 -- 数组(16)
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)的更多相关文章
- <NET CLR via c# 第4版>笔记 第16章 数组
//创建一个一维数组 int[] myIntegers; //声明一个数组引用 myIntegers = new int[100]; //创建含有100个int的数组 //创建一个二维数组 doubl ...
- Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件
如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 今天的内容是Scaffold脚手架.AppBar组件.BottomNavigationBa ...
- javaSE学习笔记(16)---网络编程
javaSE学习笔记(16)---网络编程 基本概念 如今,计算机已经成为人们学习.工作.生活必不可少的工具.我们利用计算机可以和亲朋好友网上聊天,也可以玩网游.发邮件等等,这些功能实现都离不开计算机 ...
- java JDK8 学习笔记——第16章 整合数据库
第十六章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作,称为JDBC驱动程 ...
- RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群
1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...
- 《C#本质论》读书笔记(16)构建自定义集合
16.1 更多集合接口 集合类(这里指IEnumerable层次结构)实现的接口层次结构 16.1.1 IList<T>与IDictionary<TKey,TValue> 字典 ...
- 读CLR via C#笔记
1.is 和 as 的区别 public class Employee { } a): object obj = new Employee(); if (obj is Employee) { Empl ...
- Java菜鸟学习笔记--数组篇(三):二维数组
定义 //1.二维数组的定义 //2.二维数组的内存空间 //3.不规则数组 package me.array; public class Array2Demo{ public static void ...
- php 学习笔记 数组3
15.使用数组 1).并集(union) array_merge(array1,array2,array3..) 函数把两个或多个数组合并为一个数组,后面覆盖前面 2). 交集(intersecti ...
- java笔记 -- 数组
概念: 数组是一种数据结构, 用来存储同一类型值的集合. 通过一个整型的下标可以访问数组中的每一个值. 声明: int[] a(推荐,将类型int[](整形数组)和变量名分开了) 或者int a[] ...
随机推荐
- WPF 更改 DrawingVisual 的 RenderOpen 用到的对象的内容将持续影响渲染效果
在 WPF 里面,可以通过 DrawingVisual 来进行使用底层的绘制方法,此方法需要调用 DrawingVisual 的 RenderOpen 拿到 DrawingContext 类型的对象, ...
- 2019-11-29-WPF-Process.Start-出现-Win32Exception-异常
title author date CreateTime categories WPF Process.Start 出现 Win32Exception 异常 lindexi 2019-11-29 10 ...
- Stable Diffusion中的embedding
Stable Diffusion中的embedding 嵌入,也称为文本反转,是在 Stable Diffusion 中控制图像样式的另一种方法.在这篇文章中,我们将学习什么是嵌入,在哪里可以找到它们 ...
- webpack调优技巧
webpack优化主要有三个方面:1.提高构建速度,2.减少打包体积,3.优化用户体验 提高构建速度: 启用多线程 thread-loader 使用thread-loader插件可以启用多线程进行构建 ...
- uiautomator2环境搭建+元素定位(安卓)
一.环境搭建 1.安装uiautomator2 在终端使用pip安装即可 pip install uiautomator2 2.安装adb 可参考:https://www.cnblogs.com/li ...
- R1_ES知识图谱
业务量增加,优化..优化... 学习... 学习..... 阮一鸣,eBay Pronto 平台技术负责人,管理了 eBay 内部上百个 Elasticsearch 集群,数据规模超过 4000 节点 ...
- 四、【转】基于知识图谱的推荐系统(KGRS)综述
以下文章来源于AI自然语言处理与知识图谱 ,作者Elesdspline 导语 本文是2020年针对知识图谱作为辅助信息用于推荐系统的一篇综述.知识图谱对于推荐系统不仅能够进行更精确的个性化推荐,而且对 ...
- 二:飞凌嵌入式FCU1201
场景一 充电基础设施 场景二 现代机械加工对复杂化.精密化.大型化以及自动化设备的要求不断提高,数控机床设备日益得到广泛应用.数控机床设备对加工质量及效率起着关键乃至核心作用,其造价往往相当昂贵.因此 ...
- homebrew的安装和使用
目录 背景 安装xcode 安装homebrew 有关报错解决 卸载脚本 homebrew软件搜索 brew 常用命令 brew redis安装 PhpWebStudy安装 安装php 背景 最近用b ...
- 运行程序时报go: cannot find main module, but found .git/config in
编写单元测试,运行时报下面的错误 haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/base/cheshi0 ...