自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
C# 8 中有幾個(gè)比較好玩的新特性,比如下面的這兩個(gè):System.Index 和 System.Range,分別對(duì)應(yīng)著索引和切片操作,這篇文章將會(huì)討論這兩個(gè)類的使用。

[[377601]]

本文轉(zhuǎn)載自微信公眾號(hào)「 碼農(nóng)讀書」,作者 碼農(nóng)讀書 。轉(zhuǎn)載本文請(qǐng)聯(lián)系 碼農(nóng)讀書公眾號(hào)。

C# 8 中有幾個(gè)比較好玩的新特性,比如下面的這兩個(gè):System.Index 和 System.Range,分別對(duì)應(yīng)著索引和切片操作,這篇文章將會(huì)討論這兩個(gè)類的使用。

System.Index 和 System.Range 結(jié)構(gòu)體

可以用它們?cè)谶\(yùn)行時(shí)對(duì)集合進(jìn)行 index 和 slice,下面就是 System.Index 結(jié)構(gòu)體的定義。

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

然后就是 System.Range 結(jié)構(gòu)體的定義。

  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 從尾部向前對(duì)集合進(jìn)行索引

在 C# 8.0 之前沒有任何方式可以從集合的尾部向前進(jìn)行索引,現(xiàn)在你可以使用 ^ 操作符實(shí)現(xiàn)對(duì)集合的從后往前索引,如下代碼所示:

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

接下來用一個(gè)例子來理解該操作符的使用,考慮下面的string數(shù)組。

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

接下來的代碼片段展示了如何使用 ^ 運(yùn)算符來獲取 cities 集合的最后一個(gè)元素。

  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 的最后六個(gè)字符。

  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.    } 

接下來是一個(gè)如何從 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.         } 

從圖中可以看出,輸出的數(shù)字為 1,2,3,4,即表示是一個(gè) [) 的區(qū)間。

在 C#8 之前沒有這樣非常語義化的方式對(duì)集合進(jìn)行 index 和 range,現(xiàn)在不一樣了,你可以使用 ^ 和 .. 這兩個(gè)語法糖,讓你的代碼更加干凈,可讀,易維護(hù)。

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

 

責(zé)任編輯:武曉燕 來源: 碼農(nóng)讀書
相關(guān)推薦

2021-02-01 12:36:59

C# Channels存儲(chǔ)

2021-01-18 05:18:18

C# 8模式C# 7

2021-01-19 05:30:55

C# 8異步流IEnumerable

2021-01-28 05:14:40

C#接口簽名

2020-12-31 07:31:10

C# 反射數(shù)據(jù)

2021-03-07 16:37:52

C#應(yīng)用程序

2009-08-04 10:29:06

在C#中使用存儲(chǔ)過程

2021-11-25 00:04:16

C# 插值字符串

2018-08-03 08:37:31

設(shè)計(jì)模式IT項(xiàng)目GDPR

2009-08-25 17:21:31

C#索引

2025-01-20 08:40:00

Python對(duì)象

2023-10-18 16:30:50

2015-01-27 09:16:46

DaaSDRaaS災(zāi)難恢復(fù)

2011-08-10 09:31:41

Hibernateunion

2021-03-09 07:27:40

Kafka開源分布式

2015-08-27 09:46:09

swiftAFNetworkin

2021-06-09 09:36:18

DjangoElasticSearLinux

2022-05-17 08:25:10

TypeScript接口前端

2022-06-23 08:00:53

PythonDateTime模塊

2024-01-18 08:37:33

socketasyncio線程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)