偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

[[377601]]

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

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

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

可以用它們在運行時對集合進行 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 從尾部向前對集合進行索引

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

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

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

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

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

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

譯文鏈接: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存儲

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#中使用存儲過程

2021-11-25 00:04:16

C# 插值字符串

2018-08-03 08:37:31

設(shè)計模式IT項目GDPR

2023-10-18 16:30:50

2025-01-20 08:40:00

Python對象

2009-08-25 17:21:31

C#索引

2015-01-27 09:16:46

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

2020-11-30 11:55:07

Docker命令Linux

2014-07-02 09:47:06

SwiftCocoaPods

2019-09-16 19:00:48

Linux變量

2020-04-09 10:18:51

Bash循環(huán)Linux

2024-09-06 11:34:15

RustAI語言

2024-01-18 08:37:33

socketasyncio線程

2015-08-27 09:46:09

swiftAFNetworkin
點贊
收藏

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