2

I read about indexers in MSDN - Indexers which explains how we can use objects like array with index i.e. just like normal Array. However, I think we can create array of objects like

point[] array = new point[100];

So what is the special advantages Indexer over object array?

4 Answers 4

5

If all you are after is a collection of objects then an indexer has absolutely no benefit over an array. However, if you need to store state as well as a collection, that's where an indexer shines.

For example, consider the following

public class Tree
{
    private Branch[] branches = new Branch[100];
    ...

    public string Name { get; set; }

    public Branch this[int i]
    {
        get
        {
            return branches[i];
        }
    }
}

Tree holds an internal collection but also has state of it's own. Having an indexer property allows for simple access to the underlying collection e.g.

tree.Name = "Tree";
var branch = tree[0];
Sign up to request clarification or add additional context in comments.

Comments

4

Not in this case that you have mentioned above. However, if you have anything that cannot be represented as an array will be a good example for Indexers to be used.

One .Net framework example is Dictionary. If you see the definition of Dictionary type in .Net you will find that they let you get an access of value through key. So that is a good example of using indexers where the index is presented as string.

Without indexers, how would you do that? of course by index value but it cannot be of type string then, will that be user friendly? I guess not!

So indexers gives you an opportunity to represent your code well.

enter image description here

Similarly, in case of point type, of course you can access the value of by index i.e. 0,1,2...99. What if you want to make more user friendly, such as point["x"]. That is where Indexers will help you.

Another example I could think of how about if you want to access your stack like s1 instead of push and s[0] instead of pop method.

There is a very good example of indexers by Microsoft where you can access file byte by byte by providing character location as index.

http://msdn.microsoft.com/en-us/library/aa288465(v=vs.71).aspx

Comments

2

In your line of code, you've defined an array of point objects, whatever those might be.

 point[] array = new point[100];

Assuming you have direct access to the array, you can access the first element in your array like this:

 var firstPoint = array[0];

The page you linked to is showing you how you could access that array, if it were defined inside your class, and you didn't have direct access to the array (since it's private).

For example, we could modify the example on that page to use your array:

class SampleCollection
{
    private Point[] arr = new Point[100];

    public Point this[int i]
    {
        get { return arr[i]; }
        set { arr[i] = value; }
    }
}

Then you could access the first element in the array like this:

var sc = new SampleCollection();
var item1 = sc[0];

Comments

2

That isn't an indexer.

An indexer is not used to create an array of objects, it is actually an operator overload to the '[]' operator.

An example for it's use would be if you wanted to make a List wrapper class.

In order to preserve the square braces functionality you would need (and want) to override the square braces operator. This is done via an indexer method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.