Upload
karmamen
View
218
Download
1
Embed Size (px)
DESCRIPTION
i
Citation preview
Indexers 1C# 30
C# 3.0C# 3.0
Chapter 10 Indexers
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Indexers MotivationIndexers 2C# 30
Indexers MotivationTo enable a container class to pro ide To enable a container class to provide intuitive access to its elements, C# offers an indexer
Indexers allow you to index a class or a struct Indexers allow you to index a class or a structinstance in the same way as an arrayF l k i b l i f ti i t For example: keeping boolean information in compact bit storage, while exposing it, through an indexer, as an array of bool elementsan array of bool elements
The C++ equivalent is the [] operatorq [] p
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
An Indexer ExampleIndexers 3C# 30
An Indexer Example Assume the following EmployeeManager Assume the following EmployeeManager
class that stores employeesp yclassEmployeeManager{{
privateint _numEmployees =0;privateEmployee[]_employees;
publicint NumEmployeespublicEmployeeManager(int maxEmployees)publicEmployeeManager(int maxEmployees)publicbool Add(Employeee)publicvoidSortByName()publicvoidSortBySalary()publicvoidPrint()
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
}
An Indexer ExampleIndexers 4C# 30
An Indexer Example
publicEmployeethis[int index]{
getget{if(index=_numEmployees)
returnnull;returnnull;else
return_employees[index];}set{{if(!(index=_numEmployees))
_employees[index]=value;}}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
An Indexer Example UsageIndexers 5C# 30
An Indexer Example UsageEmployeee1=newEmployee("Jack");Employeee1 newEmployee( Jack );Employeee3=newEmployee("Dave");EmployeesManager employees=newEmployeesManager(10);Employees.Add(e1);employees.Add(e2);
()employees.SortByName();employees.Print();//Herecomestheinterestingpart://Herecomestheinterestingpart:for(int i =0;i
Indexer Declaration RulesIndexers 7C# 30
Indexer Declaration RulesThe inde er bod sho ld be comprised of The indexer body should be comprised of get and set assessorsg Providing only a get or a set assessors will result in
a read-only or a write-only indexera read only or a write only indexerThe System.String class indexer, for example, is read-only, as strings are immutabley, g
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 Under the HoodIndexers 8C# 30
Indexers Under the Hood Indexers are special case of properties Indexers are special case of properties.propertyinstanceclassEmployeeItem(int32)
Lets look at their metadata definition:p p y p y ( )
{.getinstanceclassEmployeeEmployeesManager::get_Item(int32).setinstancevoidEmployeesManager::set Item(int32,classEmployee).setinstancevoidEmployeesManager::set_Item(int32,classEmployee)
}//endofpropertyEmployeesManager::Item
In C#, the name of the property is Item The getter name is get Itemg g _
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 9C# 30
Chapter 10 Exercise 1
THE WEEKLY SCHEDULE EXERCISEEXERCISE
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
SummaryIndexers 10C# 30
Summary Following are some of the indexers special 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[] t[]syntax
They are identified by their signature Properties are defined by their name
Indexers must be instance membersP ti b ith i t t ti b Properties can be either instance or static members
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel