DEV Community

Cover image for Enhancing Object Initializers with the “From-the-End” Index Operator
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

Enhancing Object Initializers with the “From-the-End” Index Operator

In the dynamic landscape of programming languages, even seemingly minor enhancements can significantly impact code readability and efficiency.

One such enhancement introduced in C# 13 is the “from-the-end” index operator (^). It allows developers to access elements from the end of an array or collection, simplifying array initialization within object initializers.

Compatibility

Keep in mind that this feature is available starting from C# 13. If you’re working with earlier versions, you’ll need to stick to indexing from the front.

Understanding the “From-the-End” Index Operator

The ^ operator, also known as the “from-the-end” index operator, provides a convenient way to access elements from the end of an array or collection. It is particularly useful when initializing arrays within object initializers.

Object Initializers in C

Before diving into the new feature, let’s revisit object initializers. In C#, object initializers allow you to create an instance of an object and set its properties or fields in a single statement. This eliminates the need for invoking constructors followed by multiple assignment statements.

Consider a simple example using a Cat class:

public class Cat
{
    public int Age { get; set; }
    public string? Name { get; set; }
}

// Using object initializer
Cat cat = new Cat { Age = 10, Name = "Fluffy" };
Enter fullscreen mode Exit fullscreen mode

In the above code, we create a Cat instance and set its Age and Name properties using an object initializer.

Leveraging the “From-the-End” Index Operator

Now let’s explore the exciting part — the use of the ^ operator within an object initializer. Imagine we have a Matrix class with an indexer that allows us to set values at specific positions:

public class Matrix
{
    private double[,] storage = new double[3, 3];
    public double this[int row, int column]
    {
        get => storage[row, column];
        set => storage[row, column] = value;
    }
}
Enter fullscreen mode Exit fullscreen mode

Suppose we want to initialize an identity matrix using the Matrix class. Traditionally, we’d index the elements from the front:

var identity = new Matrix
{
    [0, 0] = 1.0,
    [0, 1] = 0.0,
    [0, 2] = 0.0,
    // ... and so on
};
Enter fullscreen mode Exit fullscreen mode

However, with the new feature, we can use the “from-the-end” index operator to make our code more concise:

var identity = new Matrix
{
    [^0, ^0] = 1.0,
    [^0, ^1] = 0.0,
    [^0, ^2] = 0.0,
    // ... and so on
};
Enter fullscreen mode Exit fullscreen mode

By using ^0, we indicate the last element in each dimension. This not only improves readability but also aligns with our natural understanding of matrices.

C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter
Visit our other platforms: GitHub | Instagram
More content at C# Programming

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay