Information Technology Reference
In-Depth Information
public class
Customer
{
public virtual string
Name
{
get
;
protected set
;
}
// remaining implementation omitted
}
The property syntax extends beyond simple data fields. If your type should
contain indexed items as part of its interface, you can use indexers (which
are parameterized properties). It's a useful way to create a property that
returns the items in a sequence:
public int this
[
int
index]
{
get
{
return
theValues[index]; }
set
{ theValues[index] =
value
; }
}
// Accessing an indexer:
int
val = someObject[i];
Indexers have all the same language support as single-item properties:
They are implemented as methods you write, so you can apply any verifi-
cation or computation inside the indexer. Indexers can be virtual or
abstract, can be declared in interfaces, and can be read-only or read-write.
Single-dimension indexers with numeric parameters can participate in
data binding. Other indexers can use noninteger parameters to define
maps and dictionaries:
public
Address
this
[
string
name]
{
get
{
return
adressValues[name]; }
set
{ adressValues[name] =
value
; }
}
In keeping with the multidimensional arrays in C#, you can create multi-
dimensional indexers, with similar or different types on each axis:
public int this
[
int
x,
int
y]
{