IDC

如何在 C# 8 中使用 Index 和 Range

作者:admin 2021-07-02 我要评论

本文转载自微信公众号「 码农读书」,作者 码农读书 。转载本文请联系 码农读书公众号。 C# 8 中有几个比较好玩的新特性,比如下面的这两个:System.Index 和 Sy...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

本文转载自微信公众号「 码农读书」,作者 码农读书 。转载本文请联系 码农读书公众号。

C# 8 中有几个比较好玩的新特性,比如下面的这两个:System.Index 和 System.Range,分别对应着索引和切片操作,这篇文章将会讨论这两个类的使用。

System.Index 和 System.Range 结构体

可以用它们在运行时对集合进行 index 和 slice,下面就是 System.Index 结构体的定义。

  1. namespace System 
  2.     public readonly struct Index 
  3.     { 
  4.         public Index(int value, bool fromEnd); 
  5.     } 

然后就是 System.Range 结构体的定义。

  1. namespace System 
  2.     public readonly struct Range 
  3.     { 
  4.         public Range(System.Index start, System.Index end); 
  5.         public static Range StartAt(System.Index start); 
  6.         public static Range EndAt(System.Index end); 
  7.         public static Range All { get; } 
  8.     } 

使用 System.Index 从尾部向前对集合进行索引

在 C# 8.0 之前没有任何方式可以从集合的尾部向前进行索引,现在你可以使用 ^ 操作符实现对集合的从后往前索引,如下代码所示:

  1. System.Index operator ^(int fromEnd); 

接下来用一个例子来理解该操作符的使用,考虑下面的string数组。

  1. string[] cities = { "Kolkata""Hyderabad""Bangalore""London""Moscow""London""New York" }; 

接下来的代码片段展示了如何使用 ^ 运算符来获取 cities 集合的最后一个元素。

  1. var city = cities[^1]; 
  2. Console.WriteLine("The selected city is: " + city); 

下面是完整的可供参考的代码:

  1. public static void Main(string[] args) 
  2.         { 
  3.             string[] cities = { "Kolkata""Hyderabad""Bangalore""London""Moscow""London""New York" }; 
  4.  
  5.             var city = cities[^1]; 
  6.             Console.WriteLine("The selected city is: " + city); 
  7.  
  8.             Console.ReadLine(); 
  9.         } 

使用 System.Range 来提取子序列

你可以使用 System.Range 从 array 或者 span 类型上提取子集合,下面的代码展示了如何使用 range 和 index 来提取 string 的最后六个字符。

  1. class Program 
  2.    { 
  3.        public static void Main(string[] args) 
  4.        { 
  5.            string str = "Hello World!"
  6.            Console.WriteLine(str[^6..]); 
  7.  
  8.            Console.ReadLine(); 
  9.        } 
  10.    } 

接下来是一个如何从 array 上提取子集合的例子。

  1. public static void Main(string[] args) 
  2.         { 
  3.             int[] integers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
  4.             var slice = integers[1..5]; 
  5.  
  6.             foreach (int i in slice) 
  7.             { 
  8.                 Console.WriteLine(i); 
  9.             } 
  10.  
  11.             Console.ReadLine(); 
  12.         } 

从图中可以看出,输出的数字为 1,2,3,4,即表示是一个 [) 的区间。

在 C#8 之前没有这样非常语义化的方式对集合进行 index 和 range,现在不一样了,你可以使用 ^ 和 .. 这两个语法糖,让你的代码更加干净,可读,易维护。

译文链接:https://www.infoworld.com/article/3532284/how-to-use-indices-and-ranges-in-csharp-80.html


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/VW16S0a9ai4aEQNKGPxQdQ

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • []*T *[]T *[]*T 傻傻分不清楚

    []*T *[]T *[]*T 傻傻分不清楚

  • B站,牛逼!

    B站,牛逼!

  • 如何保证 Serverless 业务部署更新的一

    如何保证 Serverless 业务部署更新的一

  • Room 中的数据库自动迁移功能

    Room 中的数据库自动迁移功能

腾讯云代理商
海外云服务器