Range and index in c#8

Time:2021-10-20

Source:https://note.guoqianfan.com/2021/07/13/Range-and-Index-in-csharp

Range range

Range operator

  • Range operator:.., it generates aRangeObject.

  • Syntax:{startIndex}..{endIndex}: containsstartIndexNot includedendIndex

  • limit:0 <= startIndex <= endIndex <= arr.Length, others will report errors!

    startIndexbe equal toendIndexIf the element cannot be retrieved, anEmpty array

be careful

  • By default,..In front of the is0, followed byarr.Length(excluding)..Equivalent to0..arr.Length
  • therefore..Front and back ofCan be empty
  • therefore..It can also be used alone to represent the entire index range(0..arr.Length):arr[..]yesarrFull copy of.

Deep copystillShallow copyYou need to see what the element isValue typestillreference typeValue typeCopy value, which can be considered asDeep copyreference typeCopy reference isShallow copy

Range

RangeUsed to extract from the collectionSpecify index rangeElement to generateNew collection

establishRangeRange range = 2..4;

var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Range range = 2..4;
int[] slice = arr[range]; //  Or arr [2.. 4];
foreach (var number in slice)
{// [3, 4]
    Console.WriteLine(number);
}

arr[2..4]Express handlearrThis sequence is extracted from the element with index 2 to the element with index 4 (excluding 4) to form a new sequence. So the result is 3, 4.

Index index

IndexUsed to specifyIndexes。 The index can be used to fetch from the collectionAt specified indexThe element of isSingle element

At first glance,IndexAndIndex of type intNo difference. Um.. That’s true, becauseIndexTo be with^Operators can play a greater role. By the way, andRange。。

End operator

^Is the end operator (HAT operator), which generates aIndexObject that is used toendstartGo aheadFetch data.

AndForward fetch dataWhen the index is different from 0,^When fetching data from1At the beginning, on behalf ofPenultimate element

  • arr[^1]be equal toarr[arr.Length-1], last element
  • arr[^0]be equal toarr[arr.Length], often withRangeCombined use

If usedarr[^0]Will throwIndexOutOfRangeExceptionarr[^0]andarr[arr.Length]It means one thing.

This is a bit confusing, but in fact, other languages are almost designed in this way, for example-1This index represents the last element.

Combine range and index

Three ways to fully copy an array

int[] arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] arr01 = arr[0..arr.Length];
int[] arr02 = arr[0..^0];
int[] arr03 = arr[..];

be careful

  • The range includes start,Not includedEnd。
  • So the element with index 0 contains, index 10 or^0Element ofNot included(although it does not exist).

More examples

int[] arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] arr01 = arr[..]; // Full copy, array
int[] allButFirst = arr[1..]; // An array that does not contain the first element
int[] empty = arr[^0..]; // Empty array
int[] onlyLastItem = arr[^1..]; // An array containing only the last element
int[] last4Items = arr[^4..]; // An array containing only the last four elements
int lastItem = arr[^1]; // Last element

Use range or index alone

int[] arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Index middle = 4;
Index threeFromEnd = ^3;
Range range = middle..threeFromEnd;
int[] mySlice = arr[range]; //5, 6, 7

reference resources

  1. C# 8 – range and index:https://mp.weixin.qq.com/s?__biz=MzAwNTMxMzg1MA==&mid=2654076610&idx=1&sn=8e04e2da923f44d04c642a0e0aabd7aa, Haowen, highly recommended!!

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]