Overloading Methods

This section discusses method overloading, which is the ability to have multiple methods with the same name. Methods are overloaded by varying the number and type of parameters they accept. Listing 9.1 shows an example of method overloading.

Listing 9.1. Overloading Methods
 using System; public class Budget { enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec } enum Category { Food, Home, Jeans, Fun } 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; ...

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.