Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

C# 8.0 Index and Range Operators

12/1/2020

Comments

 
In the latest version of C# i.e. 8.0 Microsoft added two new operators i.e. Index and Range operators. Also, this features is only available in .Net Core 3. In this blog I will try to explain that in a simpler way.

Index operator (^)
Previously C# had no way of indexing the collection from the end, so with unary operator hat (^)  now developer can index from the end. Below is the example

    
One more thing to consider is that the purpose of unary operator hat (^) in regex expression is different compared to used in indexing. Index operators works for all the operators which has a Count or Length property and single integer indexer.
So it will work fine for both Array and List but won't work for Dictionary or HashSet since its a set collection i.e. unordered.

Range Operator (..)
The purpose of the range operator is to slice the collection. Below is the example

    
In range operator, start index is always inclusive and end index is always exclusive. Also range operator works only for the array and not for List

Conclusion
I personally liked both Index operator and the Range operator but bit restricted since I can't use range operator for List.
Comments
comments powered by Disqus

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot