8

Suppose I have an array or any other collection for that matter in class and a property which returns it like following:

public class Foo
{
    public IList<Bar> Bars{get;set;}
}

Now, may I write anything like this:

public Bar Bar[int index]
{
    get
    {
        //usual null and length check on Bars omitted for calarity
        return Bars[index];
    }
}
6
  • 1
    The Bars property already supports the index, so I am a bit confused about what you are trying to accomplish. Commented Jul 20, 2010 at 19:18
  • sure @Anthony Foo f=new Foo(); f.Bars[0]; wow I think I should sleep now! Commented Jul 20, 2010 at 19:20
  • 1
    @AnthonyPegram You might not want to expose Bars. It might be an implementation detail that should not be part of Foos public API. Furthermore, this exposes more than the index property of Bars. Bars may be highly stateful, and exposing it might cause a user to be able to violate the invariants that Foo promises.
    – Undreren
    Commented Aug 31, 2017 at 7:03
  • Though the other question stackoverflow.com/questions/3344620/… was asked a few days later but has more views, I'm closing this one. Commented Nov 7, 2019 at 21:05
  • See github.com/dotnet/csharplang/issues/471 for the request to add this functionality and all arguments for and against it. Until today, the developers refuse to add it because they don't see sufficient benefit for the language. Commented Mar 27, 2020 at 21:29

5 Answers 5

15

No - you can't write named indexers in C#. As of C# 4 you can consume them for COM objects, but you can't write them.

As you've noticed, however, foo.Bars[index] will do what you want anyway... this answer was mostly for the sake of future readers.

To elaborate: exposing a Bars property of some type that has an indexer achieves what you want, but you should consider how to expose it:

  • Do you want callers to be able to replace the collection with a different collection? (If not, make it a read-only property.)
  • Do you want callers to be able to modify the collection? If so, how? Just replacing items, or adding/removing them? Do you need any control over that? The answers to those questions would determine what type you want to expose - potentially a read-only collection, or a custom collection with extra validation.
7
  • Could you not copy the same style used here msdn.microsoft.com/en-us/library/146h6tk5.aspx and depending on how you implement the method get the same result?
    – Gage
    Commented Jul 27, 2010 at 17:53
  • @Gage: I'm not sure what you're saying here... that doesn't create a named indexer as far as I can see...
    – Jon Skeet
    Commented Jul 27, 2010 at 18:41
  • @JonSkeet Using foo.Bars[index], as you suggest here, will expose Bars to a consumer of Foo. That means Foo literally cannot control any invariants with respect to Bars. Furthermore, all consumers of Foo are now strongly coupled to Bars. A GetBar and SetBar method would in this case be superior, at least in terms of information hiding.
    – Undreren
    Commented Aug 31, 2017 at 7:30
  • 1
    @Undreren: It depends on whether you actually want the information to be hidden. You could also expose it as a ReadOnlyCollection<T> - or the IList<T> might already be a ReadOnlyCollection<T>. Only a getter is needed for the sake of that access - whether there's also a setter depends on requirements. I prefer not to be overly dogmatic on this front - sometimes it's simplest for all concerned to expose a setter as well as a getter; sometimes a getter that still allows collection manipulation; sometimes read-only access.
    – Jon Skeet
    Commented Aug 31, 2017 at 8:14
  • 1
    @Undreren: I'll edit the answer to mention that, but fundamentally the question is about whether there are named indexers in C#, to which the answer is no.
    – Jon Skeet
    Commented Aug 31, 2017 at 8:15
1

You can roll your own "named indexer" however. See

1

You can use explicitly implemented interfaces, as shown here: Named indexed property in C#? (see the second way shown in that reply)

1
public class NamedIndexProp
{
    private MainClass _Owner;
    public NamedIndexProp(MainClass Owner) { _Owner = Owner;
    public DataType this[IndexType ndx]
    {
        get { return _Owner.Getter(ndx); }
        set { _Owner.Setter(ndx, value); }
    }
}
public MainClass
{
    private NamedIndexProp _PropName;
    public MainClass()
    {
       _PropName = new NamedIndexProp(this);
    }
    public NamedIndexProp PropName { get { return _PropName; } }
    internal DataType getter(IndexType ndx)
    {
        return ...
    }
    internal void Setter(IndexType ndx, DataType value)
    {
       ... = value;
    }
}
2
  • 2
    You need to add some explanations to your answer to make it better
    – Ibo
    Commented Oct 18, 2017 at 0:23
  • 2
    pretty self explanatory, use the source luke!
    – Doug
    Commented Oct 18, 2017 at 3:00
0

Depending on what you're really looking for, it might already be done for you. If you're trying to use an indexer on the Bars collection, it's already done for you::

Foo myFoo = new Foo();
Bar myBar = myFoo.Bars[1];

Or if you're trying to get the following functionality:

Foo myFoo = new Foo();
Bar myBar = myFoo[1];

Then:

public Bar this[int index]
{
    get { return Bars[index]; }
}
2
  • 5
    I think 'this' is not named property.
    – Denis535
    Commented Jun 30, 2017 at 20:45
  • 2
    @wishmaster35 It isn't. I don't know why this answer was accepted, because it's a design smell: It strongly couples a consumer of Foo to Bars. In some cases this might not matter, but now Foo has no control over its own private data; everyone can modify Bars directly, around Foo.
    – Undreren
    Commented Aug 31, 2017 at 7:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.