C# 8 范围类型 Range Type:

我们最原始的代码如下:

static void Main(string[] args)
{
var myArray = new string[]
{
"Item1",
"Item2",
"Item3",
"Item4",
"Item5"
};
for(int i=1; i <= 3; i++)
{
Console.WriteLine(myArray[i]);
}
Console.ReadLine();
}

这里我们显示的定义了我们查询数组的索引1-3, 并输出他们的值。毫无疑问,当我们运行程序之后,代码结果如下:

Item2

Item3

Item4

但是,假设我们不想使用for循环,而是想要使用这个名为“range”的新特性, 我们可以将代码重写为:

static void Main(string[] args){
var myArray = new string[]
{
"Item1",
"Item2",
"Item3",
"Item4",
"Item5"
};
foreach (var item in myArray[1..3])
{
Console.WriteLine(item);
}
Console.ReadLine();
}

结果:

Item2

Item3

结果比我们预想的少了一个。这是我们使用范围类型遇到的第一个问题。

范围的起始索引是包含的,范围的结束索引是排除的

所以要想拿到正确结果应该是:

static void Main(string[] args)
{
var myArray = new string[]
{
"Item1",
"Item2",
"Item3",
"Item4",
"Item5"
};
foreach (var item in myArray[1..4])
{
Console.WriteLine(item);
}
Console.ReadLine();
}

范围缩写:

从一个索引开始到数组的最后一个对象

static void Main(string[] args)
{
var myArray = new string[]
{
"Item1",
"Item2",
"Item3",
"Item4",
"Item5"
};
foreach (var item in myArray[1..])
{
Console.WriteLine(item);
}
Console.ReadLine();
}

输出结果:

Item2

Item3

Item4

Item5

从数组的第一个对象到指定索引

foreach (var item in myArray[..3])
{
Console.WriteLine(item);
}

输出结果:

Item1

Item2

Item3

整个数组:

foreach (var item in myArray[..])
{
Console.WriteLine(item);
}

输出结果:

Item1

Item2

Item3

Item4

Item5

从数组的某个索引开始一直到距数组尾部某个索引:

操作符,操作符表示从数组末尾计算索引。

foreach (var item in myArray[1..^1])
{
Console.WriteLine(item);
}

输出结果:

Item2

Item3

Item4

范围类型

当我们编写1..4的时候,看起来就好像我们在使用新的语法,实际上这只是个语法糖,实际上它初始化一个Range类对象,就好像我们可以使用{“1”, “2”, “3”}就可以创建一个数组一样

static void Main(string[] args)
{
var myArray = new string[]
{
"Item1",
"Item2",
"Item3",
"Item4",
"Item5"
};
Range range = 1..4;
foreach (var item in myArray[range])
{
Console.WriteLine(item);
}
Console.ReadLine();
}

替代Substring方法

使用范围类型的另一个好处就是你可以使用它替换String.Substring方法, 写起来更加简单

Console.WriteLine("123456789"[1..4]);

C# 8.0 范围类型 Range Type的更多相关文章

  1. C# 8中的范围类型(Range Type)

    C# 8.0中加入了一个新的范围类型(Range Type). 这里我们首先展示一些代码,并一步一步为代码添加一些不同的东西, 为大家展示一下范围类型的功能和用法. 我们最原始的代码如下: stati ...

  2. html <input>标签类型属性type(file、text、radio、hidden等)详细介绍

    html <input>标签类型属性type(file.text.radio.hidden等)详细介绍 转载请注明:文章转载自:[169IT-最新最全的IT资讯] html <inp ...

  3. Swift编程语言学习12 ——实例方法(Instance Methods)和类型方法(Type Methods)

    方法是与某些特定类型相关联的函数.类.结构体.枚举都能够定义实例方法:实例方法为给定类型的实例封装了详细的任务与功能.类.结构体.枚举也能够定义类型方法:类型方法与类型本身相关联.类型方法与 Obje ...

  4. 父类通过泛型获得子类Class类型 以及Type体系

    1.背景介绍 在实现SSH框架中,DAO层向数据库持久化的过程中,因为大部分保存对象的方法都会调用到sava():所有索性就把save delete update select 方法进行封装到父类中, ...

  5. XAML实例教程系列 - 类型转换器(Type Converter)七

    XAML实例教程系列 - 类型转换器(Type Converter) 分类: Windows 8 Silverlight2012-06-25 13:40 961人阅读 评论(0) 收藏 举报 butt ...

  6. 类型检查 Type Checking 一些编程语言并不安全 类型化语言的优点 定型环境 (符号表) 断言的种类

    Compiler http://staff.ustc.edu.cn/~bjhua/courses/compiler/2014/ http://staff.ustc.edu.cn/~bjhua/cour ...

  7. IUnknown(TVarData(Params[0]).VPointer) as Range

    IUnknown(TVarData(Params[0]).VPointer) as Range 修改为  IUnknown(TVarData(Params[0]).VPointer) as WOrd_ ...

  8. 读书笔记 effective c++ Item 19 像设计类型(type)一样设计

    1. 你需要重视类的设计 c++同其他面向对象编程语言一样,定义了一个新的类就相当于定义了一个新的类型(type),因此作为一个c++开发人员,大量时间会被花费在扩张你的类型系统上面.这意味着你不仅仅 ...

  9. TypeError: Fetch argument 0.484375 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)

    报错: TypeError: Fetch argument 0.484375 has invalid type <class 'numpy.float32'>, must be a str ...

随机推荐

  1. spring cloud学习(一) 服务注册

    首先spring-cloud相关的简介可以去百度搜索,这里就不多说了,这里分享一个翻译spring cloud官网的中文网站spring cloud中文网 这个学习项目的代码放在 https://gi ...

  2. Mysql数据优化--DBA梳理珍藏篇

    1. 优化SQL 1)     通过show status了解各种sql的执行频率 show status like 'Com_%'        了解 Com_select,Com_insert 的 ...

  3. asp.net core 系列之用户认证(1)-给项目添加 Identity

    对于没有包含认证(authentication),的项目,你可以使用基架(scaffolder)把 Identity的程序集包加入到项目中,并且选择性的添加Identity的代码进行生成. 虽然基架已 ...

  4. (转)python的ConfigParser模块

    原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...

  5. ElasticSearch入门3: Spring Boot集成ElasticSearch

    第一步:创建项目elasticsearch 编写pom文件 <?xml version="1.0" encoding="UTF-8"?> <p ...

  6. linux使用find和crontab命令定期清理过期文件

    crontab 命令 crontab 命令是 Linux 中用来设定重复执行命令或脚本的工具.它能够在指定的时间段内,按照需求以某一时间间隔执行命令或脚本. crontab 的基本用法 crontab ...

  7. 12 二叉树-链式存储-二叉排序树(BST)

    呜呜 写这个东西花了我2天 居然花了两天!!我还要写AVL呢啊啊啊啊啊啊啊!!!!!! 等下还要跑去上自习 大早上起来脸都没洗现在先赶紧发博客 昨晚写出来了独自在其他人都睡着了的宿舍狂喜乱舞.. 迷之 ...

  8. docker使用非root用户启动容器出现“running exec setns process for init caused \"exit status 40\"": unknown”

    环境为centos7,linux内核版本为3.10 出现该问题的原因是内核3.10的bug,升级linux内核即可,升级办法如下,升级完成后重启系统,选择对应的内核版本启动即可. .导入key rpm ...

  9. mysql 导出数据时进行压缩

    mysqldump < mysqldump options> | gzip > outputfile.sql.gz 例子: mysqldump -uroot -p your_data ...

  10. C语言中求字符串的长度

    在C语言中求字符串的长度,可以使用sizeof()函数和strlen()函数,后者需要引入string.h (#include <string.h>) 因为C语言字符串是以 \0 结尾表示 ...