Method Overloading

Listing 4.15 called DirectoryCountLines(), which counted the lines of *.cs files. However, if you want to count code in *.h/*.cpp files or in *.vb files, DirectoryCountLines() will not work. Instead, you need a method that takes the file extension, but still keeps the existing method definition so that it handles *.cs files by default.

All methods within a class must have a unique signature, and C# defines uniqueness by variation in the method name, parameter data types, or number of parameters. This does not include method return data types; defining two methods that have only a different return data type will cause a compile error. Method overloading occurs when a class has two or more methods with the same name and the parameter count and/or data types vary between the overloaded methods.

Method overloading is a type of operational polymorphism. Polymorphism occurs when the same logical operation takes on many (poly) implementations (forms) because the data varies. Calling WriteLine() and passing a format string along with some parameters is implemented differently than calling WriteLine() and specifying an integer. However, logically, to the caller, the method takes care of writing the data and it is somewhat irrelevant how the internal implementation occurs. Listing 4.16 provides an example, and Output 4.10 shows the results.

Listing 4.16. Returning All the Filenames, Given a Directory

 public static class LineCounter {   public static void Main(string[] args)   {       int totalLineCount;       if (args.Length > 1)       {           totalLineCount =                                                             DirectoryCountLines(args[0], args[1]);                  }       if (args.Length > 0)       {          totalLineCount = DirectoryCountLines(args[0]);               }       else       {          totalLineCount = DirectoryCountLines();                      }       System.Console.WriteLine(totalLineCount);   }   static int DirectoryCountLines()                                 {       return DirectoryCountLines(           Directory.GetCurrentDirectory());   }   static int DirectoryCountLines(string directory)                  {       return DirectoryCountLines(directory, "*.cs");   }   static  int DirectoryCountLines(                                      string directory, string extension)   {       int lineCount = 0;       foreach (string file in           Directory.GetFiles(directory, extension))       {           lineCount += CountLines(file);       }       foreach (string subdirectory in           Directory.GetDirectories(directory))       {           lineCount += DirectoryCountLines(subdirectory);       }       return lineCount;   }   private static int CountLines(string file)   {       int lineCount = 0;       string line;       FileStream stream =           new FileStream(file, FileMode.Open);       StreamReader reader = new StreamReader(stream);       line = reader.ReadLine();       while(line != null)       {       if (line.Trim() == "")       {           lineCount++;       }       line = reader.ReadLine();    }    reader.Close();    stream.Close();    return lineCount;  } } 

Output 4.10.

 >LineCounter.exe .\ *.cs 28 

The effect of method overloading is to provide optional ways to call the method. As demonstrated inside Main(), you can call the DirectoryCountLines() method with or without passing the directory to search and the file extension.

Notice that the parameterless implementation of DirectoryCountLines() was changed to call the single-parameter version (int DirectoryCountLines(string directory)). This is a common pattern when implementing overloaded methods. The idea is that developers implement only the core logic in one method and all the other overloaded methods will call that single method. If the core implementation changes, it needs to be modified in only one location rather than within each implementation. This pattern is especially prevalent when using method overloading to enable optional parameters.

Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185 © 2008-2017.
If you may any questions please contact us: