8. Method Miscellany

In this final section, we'll look at a couple of topics that didn't fit neatly into the earlier sections.

8.1. Method Overloading

In order to call a method legally, you need to know its name, you need to know how many formal parameters it has, and you need to know the type of each parameter. This information is called the method's interface.

C# is somewhat unusual in that it allows two different methods in the same class to have the same name, provided that their parameter types are different. We say that the name of the method is overloaded because it has several different meanings. The computer doesn't get the methods mixed up. It can tell which one you want to call by the number and types of the actual parameters that you provide in the method call statement. You have already seen overloading used in the Console class. This class includes many different methods named WriteLine, for example. These methods all have different parameters, such as:

      WriteLine(int)       WriteLine(double)
      WriteLine(Object)    WriteLine(char)
      WriteLine(boolean)   WriteLine()

Of course all these different methods are semantically related, which is why it is acceptable programming style to use the same name for them all. But as far as the computer is concerned, printing out an int is very different from printing out a string, which is different from printing out a boolean, and so forth -- so that each of these operations requires a different method.

Note, by the way, that it is illegal to have two methods in the same class that have the same parameters but that have different return types. For example, it would be a syntax error for a class to contain two methods defined as:

        // CAN'T DO THIS:
        int    ReadLine() { ... }
        double ReadLine() { ... }

8.2. Organizing Programs into Several Classes and Methods

All of the programs we've seen to this point are fairly small. As programs become larger, dividing the code into methods will not be enough to organize the complexity. For one thing, keeping all the code in the file will be unwieldy, as it takes more time to scroll through in an editor and find the bit of code you want to work with.

To further organize your code, C# allows you to define more than one class in your program. Here's a short example that shows how a program can be divided up into several classes and methods.

Example 6.5. MultiClassDemo.cs

using System;

class Animals {

  static void Stomp() {
    Console.WriteLine("The elephant stomped.");
  }

  static void Meow() {
    Console.WriteLine("The cat meowed.");
  }

} // class Animals

class MultiClassDemo {
  
  // Display list of choices
  static void ShowMenu() {
    Console.WriteLine(
      "Menu\n" +
      "----\n" +
      "1) Stomp\n" +
      "2) Meow\n"
    );
  }
  
  static void Main() {
    
    ShowMenu();
    Console.Write("Enter your choice: ");
    int choice = Convert.ToInt32(Console.ReadLine());
    
    if (choice == 1) {
      Animals.Stomp();
    } else {
      Animals.Meow();
    }
  }
} // class MultiClassDemo

This program defines two classes, each containing two methods. The starting point, as usual, is the Main method. Notice that when the Main method calls the Stomp() and Meow() methods, it must prefix their names with the name of their class, since they are defined in a different class.

Normally, in a large program, you would define each class in its own source code file. If you do that, when you compile the program, you must list each source code file in the compile command, like this:

c:\myprog> csc MultiClassDemo.cs Animals.cs