C# 8.0 New Feature - Ranges And Indices

Introduction

In this article, we are going to learn the new features of C# 8.0, Ranges, and Indices. This introduces new operators and new types - Systems. Index and System.Range.

System.Range

In C#, there is no way to perform the 'slice' or 'ranges' for collections. There is no way to skip and take data without using LINQ methods. So, there's a new way, i.e., using the new range operator 'x..y'. We call this '..' operator the 'Range Operator'. In the above example, 'x' is nothing but the starting index, and 'y' is the ending index. These are also called inclusive and exclusive.

An example of using LINQ to get the elements 2nd, 3rd, and 4th.

using System;
using System.Linq;
class Program
{
    static void Main()
    {
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };

        var techSlice = techArray.Skip(1).Take(3).ToArray();
        foreach (var item in techSlice)
        {
            Console.WriteLine(item); // Output: C++ C# F#
        }
    }
}

An example of using the 'range operator'(x..y) to get the elements 2nd, 3rd, and 4th.

using System;
class Program
{
    static void Main()
    {
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };

        foreach (var item in techArray[1..4])
        {
            Console.WriteLine(item); // Output: C++ C# F#
        }
    }
}

  An example of defining this 'x..y' operator with the 'Range' type as follows.

using System;
class Program
{
    static void Main()
    {
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };

        Range range = 1..4;
        foreach (var item in techArray[range])
        {
            Console.WriteLine(item); // Output: C++ C# F#
        }
    }
}

System.Index

This is the second new type introduced in C# 8.0. We have seen a couple of examples about 'Range'. Here, in the 'x..y'(range operator) - x and y are the types of 'Index'. Range internally uses the 'Index' type.

Let's see how to use the Index type with the range operator.

using System;
class Program
{
    static void Main()
    {
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };

        Index startIndex = 1;
        Index endIndex = 4;
        foreach (var item in techArray[startIndex..endIndex])
        {
            Console.WriteLine(item); // Output: C++ C# F#
        }
    }
}

The Hat(^) operator

This is the second new operator introduced in C# 8.0, named the 'Hat'(^) operator. What can we do with this operator? For example - get the last element from the array currently, we are doing like as follows.

using System;
class Program
{
    static void Main()
    {
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };
        var lastOldWay = techArray[techArray.Length - 1];
        Console.WriteLine("Old way: " + lastOldWay + "(techArray[techArray.Length - 1])"); // Output: Old way: GraphQL(techArray[techArray.Length - 1])
    }
}

Using the hat(^) operator.

using System;
class Program
{
    static void Main()
    {
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };
        var lastNewWay = techArray[^1];
        Console.WriteLine("New way: " + lastNewWay + "(techArray[^1])"); // Output: New way: GraphQL(techArray[^1])
    }
}

Using Ranges for strings to get the substring.

using System;
class Program
{
    static void Main()
    {
        var welcome = "Welcome to C# Corner!";
        Console.WriteLine(welcome[^10..]); // Output: C# Corner!
    }
}

Following are some more examples of using new types and operators.

Example 1. Use Ranges and Index in the array.

using System;
class Person
{
    public string FirstName { get; }
    public string LastName { get; }

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}
class Program
{
    static void Main()
    {
        var people = new Person[]
        {
            new Person("Mangesg", "G"),
            new Person("Jeetendra", "G"),
            new Person("Avinash", "J"),
            new Person("Sai", "B")
        };
        foreach (var per in people[1..^2])
        {
            Console.WriteLine(per.FirstName + ' ' + per.LastName); // Output: Jeetendra G
        }
    }
}

Example 2. Exclude the first element.

using System;
class Program
{
    static void Main()
    {
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };
        foreach (var item in techArray[1..])
        {
            Console.WriteLine(item);
        }
    }
}

Example 3. Get the first 3 elements.

using System;
class Program
{
    static void Main()
    {
        // Array containing programming languages
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };
        // Loop through the first 3 elements of the techArray and print each element
        foreach (var item in techArray[..3])
        {
            Console.WriteLine(item); // Output: C, C++, C#
        }
    }
}

Example 4. Exclude the first and last element.

using System;
class Program
{
    static void Main()
    {
        // Array containing programming languages
        string[] techArray = { "C", "C++", "C#", "F#", "JavaScript", "Angular", "TypeScript", "React", "GraphQL" };
        // Loop through elements from index 1 to the second last element and print each element
        foreach (var item in techArray[1..^1])
        {
            Console.WriteLine(item);
        }
    }
}

Example 5. Getting substring from the string directly.

using System;
class Program
{
    static void Main()
    {
        // Extract the substring from the beginning up to index 3 and print it
        Console.WriteLine("Jeetendra"[..4]); // Output: Jeet
    }
}

You can also download all these examples from here.

Summary

In this article, we discussed what Ranges and Indices in C# 8.0 are and how to use this with examples. If you have any suggestions or queries regarding this article, please contact me.

Stay tuned for other concepts of C# 8.0.

“Learn It, Share it.”


Similar Articles