PART I
C h a p t e r 1 0 :
I n d e x e r s a n d P r o p e r t i e s
251
PART IPART I
Console.WriteLine("fs[2]: " + fs[2]);
Console.WriteLine("fs[1.1]: " + fs[1.1]);
Console.WriteLine("fs[1.6]: " + fs[1.6]);
}
}
This program produces the following output:
fs[1]: 1
fs[2]: 2
fs[1.1]: 1
fs[1.6]: 2
As the output shows, the
double
indexes are rounded to their nearest integer value.
Specifically, 1.1 is rounded to 1, and 1.6 is rounded to 2.
Although overloading an indexer as shown in this program is valid, it is not common.
Most often, an indexer is overloaded to enable an object of a class to be used as an index,
with the index computed in some special way.
Do'stlaringiz bilan baham: |