C# 8.0中的范围类型(Range Type)示例详解
作者:LamondLu 时间:2023-04-11 22:26:10
前言
C# 语言是在2000发布的,至今已正式发布了7个版本,每个版本都包含了许多令人兴奋的新特性和功能更新。同时,C# 每个版本的发布都与同时期的 Visual Studio 以及 .NET 运行时版本高度耦合,这也有助于开发者更好的学习掌握 C#,并将其与 Visual Studio 以及 .NET 的使用结合起来。
C# 8.0中加入了一个新的范围类型(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
从数组的某个索引开始一直到距数组尾部某个索引
C# 8.0提供了^操作符,^操作符表示从数组末尾计算索引。
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]);
来源:https://www.cnblogs.com/lwqlun/p/10095821.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
查找算法之二分查找的C++实现
C# Pointer指针应用实例简述
C# Distinct和重写IEqualityComparer时要知道的二三事
IDEA不编译除了.java之外的文件的解决办法(推荐)
![](https://img.aspxhome.com/file/2023/0/58570_0s.png)
c#正反序列化XML文件示例(xml序列化)
SpringBoot之logback-spring.xml不生效的解决方法
![](https://img.aspxhome.com/file/2023/0/128930_0s.png)
java 中使用匿名类直接new接口详解及实例代码
简单谈谈Java中的栈和堆
![](https://img.aspxhome.com/file/2023/3/61163_0s.png)
Flutter加载图片流程之ImageProvider源码示例解析
汉字转拼音缩写示例代码(Silverlight和.NET 将汉字转换成为拼音)
C#中DataGridView的样式
C++实现TCP客户端及服务器Recv数据筛选处理详解
![](https://img.aspxhome.com/file/2023/9/114899_0s.jpg)
java poi设置生成的word的图片为上下型环绕以及其位置的实现
![](https://img.aspxhome.com/file/2023/5/76285_0s.png)
Spring如何使用@Indexed加快启动速度
![](https://img.aspxhome.com/file/2023/6/62236_0s.jpg)
Spring Cloud Ribbon配置详解
springboot bootcdn使用示例详解
SrpingDruid数据源加密数据库密码的示例代码
![](https://img.aspxhome.com/file/2023/1/63021_0s.png)
spring boot项目中MongoDB的使用方法
Android中应用界面主题Theme使用方法和页面定时跳转应用
![](https://img.aspxhome.com/file/2023/3/92613_0s.gif)