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:

...accounts[4]...

So if an ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.