日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何在 C# 8 中使用 Index 和 Range

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

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供未央企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都做網(wǎng)站、html5、小程序制作等業(yè)務(wù)。10年已為未央眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

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); 

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

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

接下來(lái)的代碼片段展示了如何使用 ^ 運(yùn)算符來(lái)獲取 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 來(lái)提取子序列

你可以使用 System.Range 從 array 或者 span 類型上提取子集合,下面的代碼展示了如何使用 range 和 index 來(lái)提取 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.    } 

接下來(lái)是一個(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 之前沒有這樣非常語(yǔ)義化的方式對(duì)集合進(jìn)行 index 和 range,現(xiàn)在不一樣了,你可以使用 ^ 和 .. 這兩個(gè)語(yǔ)法糖,讓你的代碼更加干凈,可讀,易維護(hù)。

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


文章名稱:如何在 C# 8 中使用 Index 和 Range
文章網(wǎng)址:http://www.dlmjj.cn/article/ccocgdi.html