3

I want to implement a property that returns a value based on the index it receives. I am not just encapsulating a private array. In fact, the data I will be returning is not stored in any arrays, but instead stored in member objects. This array property will simply be a way to access this data in an indexed way without needing to store it in an indexed way.

According to this article, the following should work:

public double Angles[int i] 
{
    get { // return a value based on i; }
}

I get the following error, however:

The type or namespace 'i' could not be found (are you missing a using directive or an assembly reference?)
Invalid token ']' in class, struct or interface member declaration
Invalid expression term 'int'
Bad array declarator: To declarate a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

From those errors, I think it seems that the compiler thinks I am attempting to create an array member. Obviously my syntax is wrong here. Can anybody tell me the correct way to do this?

1
  • seaerch for indexers in C# Commented Apr 10, 2013 at 9:06

2 Answers 2

4

Named indexers do not exist in C#. You can, however, add Angles as some type of object that has an indexer, i.e.

public class Foo {
    public Angles Angles { get { return angles; } }
    ...
}

...

public class Angles {
    public double this[int index] { get { ... } }
    ...
}

Or if you want the implementation in one class:

public class Foo : IAngles {
    public IAngles Angles { get { return this; } }
    double IAngles.this[int index] { get { ... } }
}

public interface IAngles {
    double this[int index] { get;}
}
Sign up to request clarification or add additional context in comments.

5 Comments

Cool idea with the explicitly implemented interface!
@DanielHilgarth I almost wonder whether it should be IIndexer<int,double> though.
That would be problematic if there would be multiple named indexers with the same types in one class. Another alternative would be to use nested types if they really only exist to implement a named indexer.
@DanielHilgarth indeed - a struct that contains only a reference to the "outer" object might be a very efficient option.
Good idea. I think that would actually be the best solution, if - again - those types only exist to implement a named indexer. If those types have any other public methods or properties beside the indexer, they should be normal public classes.
1

The method has to looke like that:

public double this[int i] 
{
    get { // return a value based on i; }
}

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.