Overloading Indexers

Indexers are overloaded similar to methods. By providing different numbers and types of parameters, indexers can be very flexible. Listing 9.2 shows an example of indexer overloading.

Listing 9.2. Overloading Indexers
 using System; public enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec } public enum Category { Food, Home, Jeans, Fun } public class Budget { decimal[,] myBudget = new decimal[4, 12]; public void Initialize(decimal[] initVals) { for (Category c=Category.Food; c <= Category.Fun; c++) { for (Month m=Month.Jan; m <= Month.Dec; m++) { myBudget[(int)c, (int)m] = initVals[(int)m]; } } } public void Initialize(decimal initVal) { for (Category c=Category.Food; c <= Category.Fun; c++) { for (Month ...

Get C# Unleashed 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.