Understanding Ranges, Indices, and Static Local Functions in C#

16 June 2021 at 10:00 by ParTech Media - Post a comment

There are several programming languages available in the market to implement your requirements. There are so many external factors that you might end up considering while choosing a programming language. For example, the language should have a good ecosystem, community support, and the availability of people who can work on it.

Along with these, it also matters what the programming language offers in addition to these to the developers. Does it provide the ability to achieve some of the common and complex operations in a faster manner? And does it have the common features that are found in powerful programming languages? These things make the life of the developers a way better.

And when it comes to common operations, Python has a wide set of operations that are quite helpful in working with collections and retrieving data from collections. Similar in C# (after version 8), a set of new features were introduced for doing operations with collections. Let’s understand in detail these features and how helpful they are when it comes to the implementation part.

Table of contents

  1. Understanding Ranges and Indices
  2. Understanding Static Local functions
  3. Conclusion

Understanding Ranges and Indices

Index and Ranges are used to access data from collections in C# in a quicker manner.

Indexes are present in the namespace System.Index. As the name suggests, Indexes are used to access the element by its position from the collection. Let's understand how this was done before the new technique was introduced and then compare it with what is possible today.

Consider we need to access the last element from the collection.


var collection = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8" };


var data = collection[(collection.Count - 1)];

After the new technique,

var data = collection[^1];

Also, to save the value of the Index with the hat operator, a variable of type Index can be used. And later the variable can be used to get the particular Index.

Index ind = ^4;

var data = collection[ind];

Here, the hat(^) operator indicates the Index from the last. And when we use the hat operator, it does the same logic of count - 1. So when we use ^0, it means it would throw an error as the Index begins from 0 and ends with count - 1. Similarly, to access second last, third last and so on, the hat operator with the respective numbers can be used.

So far, we have seen what an Index is and how it can help us in getting the element from the reverse without doing any calculations. Let's now see about Ranges.

Ranges are from the namespace System.Range and it is one step ahead of the Index. If the Index is being used to fetch a particular element from a collection, then Range is used to fetch a series of elements from the collection. As we cannot apply the range for Lists, we are proceeding with an array for the current one.

For example, If we need to get the elements from 3 to 5, then in the existing code we can get the data with the below code.


With the introduction of Ranges, it is easier to get the series of elements from the collection. Here is how it is done -

var result = collection[3..5];

The above range starts printing from Index number 3 and prints up to one before Index number. So here we get two elements from the collection.

Similarly, Ranges can be implemented in switch cases. Say if we have a set of sequence which has to do a similar job, then,

Switch (statement)


​    	Case 1..10:

​          	//do this

​    	Case 11..20:

​          	//do this


Here in the above example, statements that are between 1 and 10 will execute the code below that. Similarly, statements from 11 to 20 will execute the same code.

C# does not stop from providing the Index and Range features individually. Instead, it provides the option to use them together in the code. Let's now see how it will be handy while developing.

We saw in Range how to write Skip 3 and Take 2. The same can be achieved by using both of them together.

var data = collection[3..^3];

Here the start Index is from 3, then it goes up to the fourth last element in the collection.

Similarly, below are some scenarios that can be achieved through the combination of Range and Index together.

  1. Skip the last three elements from the collection.
var data = collection[..^3];
  1. To Return the last two elements from the collection.
var data = collection[^2..^0] 

Here, the ^0 will not be throwing an error, as we know in Range, the element before the last specified element will be taken into consideration.

Understanding Static Local functions

In C# 7.0, a new feature was introduced which allowed the developers to create a local function within a method. This helped developers achieve required functionality from it. Here is an example -

String Method2()


​    	int variable;

​    	LocalFunction();

​    	return variable;

​    	LocalFunction() => variable = 0;


In the above example, the variable will be overridden inside the local function which affects the original value and the place where it is being used. To avoid that, static local functions were introduced as a part of C# 8.0. This ensured that the variables that are present in the enclosed methods are not being affected because of the changes that happened within the local function.

String Method2()


​    	int firstVariable = 5 ;

        int secondVariable = 6 ;

​    	return LocalFunction(firstVariable + secondVariable);

​    	Static LocalFunction(int left, int right) => left + right;


In the above example, the input parameter cannot be altered inside the local function that has been enclosed inside the Method2.


Static local functions, Indices, and Ranges are some of the most useful and handy features that were released as a part of C# 8.0. Using them in your application saves a lot of time for you and your developers.