Indexers: Using Objects Like Arrays

As we have seen earlier, an array contains many useful built-in methods and properties, such as Sort and Length. Sometimes, however, we need other abilities than those offered by the Array class. Perhaps we want an array that can find the sum and the average of its elements or display their values on a graphical user interface in a specific style. In those cases, we can write a class that essentially is an array (it would likely have an array or another collection as an instance variable) but with the extra functionality included.

Remember that to access a specific element of an array, we insert the element's index number in square brackets after the name of the array as follows:


So if an ...

Get C# Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.