C# Indexer (Indexer)
Indexer (Indexer) allows an object to be accessed using a subscript like an array.
When you define an indexer for a class, the class behaves like a virtual array . You can use the array access operator [ ] to access the members of that class.
Syntax
The syntax of a one-dimensional indexer is as follows:
element
type
this
[
int
index
]
{
// get accessor
get
{
// Return the value specified by index
}
// set accessor
set
{
// Set the value specified by index
}
}
Uses of Indexers
The declaration of an indexer's behavior is somewhat similar to that of a property. Like a property, you can use get and set accessors to define an indexer. However, a property returns or sets a specific data member, while an indexer returns or sets a specific value of an object instance. In other words, it divides the instance data into smaller parts and indexes each part, getting or setting each part.
Defining a property includes providing the property name. An indexer is defined without a name, but with the this keyword, which points to the object instance. The following example demonstrates this concept:
Example
using
System
;
namespace
IndexerApplication
{
class
IndexedNames
{
private
string
[
]
namelist
=
new
string
[
size
]
;
static
public
int
size
=
10
;
public
IndexedNames
(
)
{
for
(
int
i
=
0
;
i
<
size
;
i
++
)
namelist
[
i
]
=
"N. A."
;
}
public
string
this
[
int
index
]
{
get
{
string
tmp
;
if
(
index
=
0
&&
index
<=
size
1
)
{
tmp
=
namelist
[
index
]
;
}
else
{
tmp
=
""
;
}
return
(
tmp
)
;
}
set
{
if
(
index
=
0
&&
index
<=
size
1
)
{
namelist
[
index
]
=
value
;
}
}
}
static
void
Main
(
string
[
]
args
)
{
IndexedNames names
=
new
IndexedNames
(
)
;
names
[
0
]
=
"Zara"
;
names
[
1
]
=
"Riz"
;
names
[
2
]
=
"Nuha"
;
names
[
3
]
=
"Asif"
;
names
[
4
]
=
"Davinder"
;
names
[
5
]
=
"Sunil"
;
names
[
6
]
=
"Rubic"
;
for
(
int
i
=
0
;
i
<
IndexedNames
.
size
;
i
++
)
{
Console
.
WriteLine
(
names
[
i
]
)
;
}
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
Overloading Indexers
Indexers can be overloaded. An indexer declaration can also have multiple parameters, and each parameter can be of a different type. There is no need for an indexer to be of integer type. C# allows indexers to be of other types, for example, string type.
The following example demonstrates overloading an indexer:
Example
using
System
;
namespace
IndexerApplication
{
class
IndexedNames
{
private
string
[
]
namelist
=
new
string
[
size
]
;
static
public
int
size
=
10
;
public
IndexedNames
(
)
{
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
namelist
[
i
]
=
"N. A."
;
}
}
public
string
this
[
int
index
]
{
get
{
string
tmp
;
if
(
index
=
0
&&
index
<=
size
1
)
{
tmp
=
namelist
[
index
]
;
}
else
{
tmp
=
""
;
}
return
(
tmp
)
;
}
set
{
if
(
index
=
0
&&
index
<=
size
1
)
{
namelist
[
index
]
=
value
;
}
}
}
public
int
this
[
string
name
]
{
get
{
int
index
=
0
;
while
(
index
<
size
)
{
if
(
namelist
[
index
]
==
name
)
{
return
index;
}
index
++;
}
return
index
;
}
}
static
void
Main
(
string
[
]
args
)
{
IndexedNames names
=
new
IndexedNames
(
)
;
names
[
0
]
=
"Zara"
;
names
[
1
]
=
"Riz"
;
names
[
2
]
=
"Nuha"
;
names
[
3
]
=
"Asif"
;
names
[
4
]
=
"Davinder"
;
names
[
5
]
=
"Sunil"
;
names
[
6
]
=
"Rubic"
;
// Use the first indexer with an int parameter
for
(
int
i
=
0
;
i
<
IndexedNames
.
size
;
i
++
)
{
Console
.
WriteLine
(
names
[
i
]
)
;
}
// Use the second indexer with a string parameter
Console
.
WriteLine
(
names
[
"Nuha"
]
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2