Sie sind auf Seite 1von 4

All about Indexers

by: Rincewind

All about Indexers

by: Rincewind

Introduction

Indexers are one neat feature of C#. They allow us to access a particular class as it is an array. They are
similar to the properties in means that they encapsulate method calls into more convenient representation
of value access and value setting.

Let me remind you the format of declaring a property:


private int vAge;
public int age
{
get
{
return vAge * 7;
}
set
{
vAge = value;
}
}

Here the get and set accessors do simple tasks but they can be as complex as needed – database access,
file operations, network transfers and whatever you can think of.

Notice that the property is declared as public and the field vAge is private thus making it inaccessible
from anywhere except from the property (and the same class of course).

Here is the source code of an indexer:


class Articles
{
const int max = 1000;
private string [] texts = new string[max];
public string this [int index]
{
get
{
if (index < 0)
return "negative";
else if (index > max)
return "Infinity";
else
return texts[index];
}
set
{
if ((index >= 0) && (index <= max))
texts[index] = value;
}
}
}
Indexers are similar to properties as they both have get and set accessors, but it is important to
understand the differences between them.

First, declaring a property you can access it with its name, so it behaves like variable. The indexer is
actually a class so it behaves as a type, i.e. you have to declare a variable of its type (actually to instantiate
an object from it, but all variables are objects anyway).
age = 5;
But
Articles articles = new Articles();
Articles[5] = “Ankh-Morpork”;

Second, indexers are only part of a class and do not exist alone. The important line is
public string this [int index]

This is the declaration of the indexer, then follows the getter and the setter similar to those of the
property with the only difference the index value which you have to take in account when setting and
getting from the internal structure.

You see that we have declared the string array ‘texts’ as private so it is inaccessible from outside the
class. Do this always.

Again, your internal data can be whatever you want (or whatever you can code).

If you look at the indexers again you may notice that the same thing can be accomplished by simply
using a getValue(int index) method and setValue(int index, Object value) method.

While this is true, and this is the way it would be done if we were using Java, indexers are one great
layer of abstraction over our code.
Here are some suggestions on indexers’ usage:

* Use the set accessor to check if the new value is correct and if not to proceed properly (throw an
exception, set some empty value or do nothing). This way you are always sure that the stored internal data
is always correct.

* They help you to represent as array values which may have no similarities to arrays as internal
representation, but is more convenient to think about them as arrays.

* You are not limited to using integers as indexes. You can use strings to achieve the result used in the
Hash class and many others. Looks like
table[“Users”] = otherTable

Of course you can use whatever type you want.

* If you don’t provide a setter, you make the indexer a read-only. But don’t do this because it does not
make much sense, since arrays are read and write variables. You can use a method instead to get the value
based on the index.

* You can provide more than one indexer. You may want to access tables by their index or by their
name. If this is the case, just create one indexer with integer parameter and a string parameter.

And one last thing to remember: use indexers only when it makes sense, when the representation of the
class data as multiple objects is proper.
Comparison Between Properties and Indexers
by: mosessaur

Comparison Between Properties and Indexers

by: mosessaur

Indexers permit instances of a class or struct to be indexed in the same way as arrays. Indexers are similar
to properties. Except for the differences shown in the following table, all of the rules defined for property
accessors apply to indexer accessors as well.

Property:

-Identified by its name.

-Accessed through a simple name or a member access.

-Can be a static or an instance member.

-A get accessor of a property has no parameters.

-A set accessor of a property contains the implicit value parameter.


Indexer: -Identified by its signature.

-Accessed through an element access.

-Must be an instance member.

-A get accessor of an indexer has the same formal parameter list as the indexer.

-A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value
parameter.

Indexer Declaration:

General:
accessModifier returnType this[IndexType1 indexName1,
IndexType2 indexName2, IndexType3 indexName3, .....]
{
get
{
//use indexName1, indexName2, indexName3 .. to get data
}
set
{
//use indexName1, indexName2, indexName3 .. to set data
}
}

Das könnte Ihnen auch gefallen