Access collection elements with the Dot (.) operator

  • Thread starter Thread starter Ana Lindt
  • Start date Start date
A

Ana Lindt

Hello,

I remember reading in one of my books that it was possible to access some
collection's elements with the dot (.) operator just like if they were
properties. The problem is I don't remember what this kind of operation was
called thus making it difficult to find documentation about it.

I would apreciate if someone could just tell me the name of this operation
(of implementing collections with the dot operator) or the name of the
interface I must implement - anything that can help me find where I read
about it in the first place.

Thanks in advance,

Ana L.
 
Hello Ana,

You might have read that it is possible to access elements of a collection
WITHOUT the dot operator.
In other words, instead of writing something like

myCollection.GetItemAt(myItemIndex)

it is possible to write

myCollection[myItemIndex].

To achieve this, you should implement a so called indexer. It looks like a
property, but is named "this". For example:

public object this[int index]
{
get
{
return _storage[index];
}
}
 
Back
Top