You are on page 1of 5

Indexers 1

C# 30

C# 3.0
Chapter 10 Indexers

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 2

C# 30

Indexers Motivation
To enable a container class to pro
provide
ide
intuitive access to its elements, C# offers
an indexer
Indexers allow you to index a class or a struct
instance in the same way as an array
For
F example:
l keeping
k
i boolean
b l
i f
information
ti in
i compactt
bit storage, while exposing it, through an indexer, as
an array of bool elements

The C++ equivalent


q
is the [] operator
p

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 3

C# 30

An Indexer Example
Assume the following EmployeeManager
class that stores employees
p y
classEmployeeManager
{
privateint _numEmployees =0;
privateEmployee[]_employees;
publicint NumEmployees
publicEmployeeManager(int maxEmployees)
maxEmployees)
publicbool Add(Employeee)
publicvoidSortByName()
publicvoidSortBySalary()
publicvoidPrint()
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 4

C# 30

An Indexer Example
publicEmployeethis[int index]
{
get
{
if(index<0||index>=_numEmployees)
returnnull;
else
return_employees[index];
}
set
{
if(!(index<0||index>=_numEmployees))
_employees[index]=value;
}
}

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 5

C# 30

An Indexer Example Usage


Employeee1=newEmployee("Jack");
Employeee1
newEmployee( Jack );
Employeee3=newEmployee("Dave");
EmployeesManager employees=newEmployeesManager(10);
Employees.Add(e1);
employees.Add(e2);
employees.SortByName();
()
employees.Print();
//Herecomestheinterestingpart:
for(int i =0;i <employees.NumEmployees;++i)
{
currentSalary =employees[i].Salary;
employees[i].Salary +=currentSalary*5/100;
}

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 6

C# 30

Indexer Declaration Rules


The indexer declaration is of the form:
[
] [
] yp this [
p
]
[attributes][modifiers]type
[indexparameterlist]

[attributes]

Additional declarative information

[modifiers]

Can be new or one of the access modifiers

type

Th type
The
t
handled
h dl d b
by th
the iindexer
d
((get
t and
d set)
t)

[indexparameterlist]

At least one parameter must be specified

publicEmployeethis[int index]
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 7

C# 30

Indexer Declaration Rules


The inde
indexer
er body
bod sho
should
ld be comprised of
get and set assessors
g
Providing only a get or a set assessors will result in
a read-only
read only or a write
write-only
only indexer
The System.String class indexer, for example, is
read-only,
y, as strings
g are immutable

Indexers can be overloaded:


You can define more than one indexer for a class, as
long as they differ in their parameter lists

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 8

C# 30

Indexers Under the Hood


Indexers are special case of properties
.propertyinstanceclassEmployeeItem(int32)
p p
y
p y
(
)
{
.getinstanceclassEmployeeEmployeesManager::get_Item(int32)
.setinstancevoidEmployeesManager::set Item(int32,classEmployee)
.setinstancevoidEmployeesManager::set_Item(int32,classEmployee)
}//endofpropertyEmployeesManager::Item

Lets look at their metadata definition:

In C#, the name of the property is Item


The g
getter name is g
get_Item
It receives one input parameter: the index

The setter name is set_Item


_
It receives two input parameters: the index & the value

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 9

C# 30

Chapter 10 Exercise 1

THE WEEKLY SCHEDULE


EXERCISE
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Indexers 10

C# 30

Summary
Following are some of the indexers special
characteristics:
They have (in C#) an hard coded name: Item
They have an optional (greater than zero) number of
parameters
They are accessed through the array element access
[]
[]syntax
t
They are identified by their signature
Properties are defined by their name

Indexers must be instance members


Properties
P
ti can be
b either
ith iinstance
t
or static
t ti members
b

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

You might also like