greenoldman
greenoldman

Reputation: 21062

What is “default indexed property” in CLR/C#?

I am reading "Framework Design Guidelines" book and I am puzzled by one of the rules (p.142) "Do not use non-default indexed properties."

I am confused, for several reasons -- indexed property should not mean indexer, but if it is about a property what is default property (auto one?), if this is a typo ("indexeR property"), and it is about indexers, I found some hints, that maybe indexer with single int parameter is called a default one (is it?), but in such case C# does not forbid other ones.

I am unable to decipher this.

Upvotes: 3

Views: 666

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

In some languages (e.g. Delphi) you can define named indexed property(ies), e.g.

  type 
    TMyCollection = class(TObject)
    private
      ...
    protected 
      function GetItem(idx: LongInt): LongWord; virtual;
    public
      // Named ("Items") indexed (note "idx") property 
      property Items[idx: LongInt]: LongWord read GetItem; 
    end;

CLR supports this construction

https://msdn.microsoft.com/en-us/library/vstudio/ms229061(v=vs.100).aspx

however, it doesn't recommended to use such a construction, and some languages (C# being an example) enforce this guideline. All we are allowed in C# is nameless (default) index property - indexer.

Upvotes: 4

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239754

C# Indexers are always default indexed properties. It is not possible to create a non-default indexed property in C#. In Visual Basic, it's perfectly possible to create non-default properties that use indexes.

Since there's no way for a C# program to make use of a non-default indexed property, that's why the framework design guidelines advise against creating them - so that C# and VB code can interoperate.

Upvotes: 1

Related Questions