Monday, December 30, 2019

Index Range C#


C# 8 introduced a new syntax for expressing a range of values using 2 System objects namely "System.Index and System.Range". 

They are useful to index/slice C# collections during the runtime of an application.

System.Index

New Index expression of C#, refers "from the end".   This feature introduces a new unary prefix hat(^) operator with the better way to index a collection from the end.

General code syntax is represented as
System.Index operator ^(int fromEnd);

Sample C# Index code is
var lastItem = array[^1];

The above code is equivalent to the tradition code of
var lastItem = array[collection.Count-1]; 

System.Range

Ranges way to access ranges / slices of any collections with an introduction of new range operation ..

The starting index of a range is inclusive, and the ending index is exclusive. Alternatively, the ending can be specified as an offset from the end.

System.Range operator ..(Index start = 0, Index end = ^0);

Sample C# Range code is
var lastItem = array [^1];
var subCollection = array[2..^5];


Above code is equivalent to the traditional way of C# code as
var lastItem = array [array.Count -1];
var subCollection = array.ToList().GetRange(2, 4);


This will help you to avoid LINQ and making your code compact and more readable.

7 comments:

  1. I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.

    Data Engineering Services 

    Data Analytics Solutions

    Artificial Intelligence Solutions

    Data Modernization Services

    ReplyDelete
  2. I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.

    Data Engineering Services 

    Data Analytics Solutions

    Artificial Intelligence Solutions

    Data Modernization Services

    ReplyDelete
  3. I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.

    Data Engineering Services 

    Data Analytics Solutions

    Artificial Intelligence Solutions

    Data Modernization Services

    ReplyDelete
  4. I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.

    Data Engineering Services 

    Data Analytics Solutions

    Artificial Intelligence Solutions

    Data Modernization Services

    ReplyDelete
  5. I appreciate you taking the time and effort to share your knowledge. This material proved to be really efficient and beneficial to me. Thank you very much for providing this information. Continue to write your blog.

    Data Engineering Services 

    Data Analytics Solutions

    Artificial Intelligence Solutions

    Data Modernization Services

    ReplyDelete