Method Overloading

This section describes what is method overloading - multiple methods in the same class with the same name but different method signatures.

A method is overloaded if there is another method with the same method name but a different method signature existing in the same class. A method signature includes the following elements:

and excludes the following elements:

The following program shows the how a method can be overloaded differently:
// Overloads.cs
// Copyright (c) 2008 HerongYang.com. All Rights Reserved.

class Overloads {
   public static void Main() {
      Print(1);
      Print(12345678901234);
      Print((object)2);
      Print(3, 4);
      Print(new int[]{5,6});
   }
   public static void Print(object o) {
      System.Console.WriteLine("Print(object o): {0}.", o.ToString());
   }
   public static void Print(int i) {
      System.Console.WriteLine("Print(int i): {0}.", i);
   }
   public static void Print(long i) {
      System.Console.WriteLine("Print(long i): {0}.", i);
   }
   public static void Print(ref int i) {
      System.Console.WriteLine("Print(ref int i): {0}.", i);
   }
   public static void Print(int i, int j) {
      System.Console.WriteLine("Print(int i, int j): {0}, {1}.", i, j);
   }
//   public static void Print(int[] a) {
//      // identical to Print(params int[] a)
//      System.Console.WriteLine("Print(int[] a): {0}, {1}.", a[0],
//         a[1]);
//   }
   public static void Print(params int[] a) {
      System.Console.WriteLine("Print(params int[] a): {0}, {1}.",
         a[0],a[1]);
   }
}

Output:

Print(int i): 1.
Print(long i): 12345678901234.
Print(object o): 2.
Print(int i, int j): 3, 4.
Print(params int[] i): 5, 6.

Two interesting notes:

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

Class Features

 "const" and "readonly" Variables

Method Overloading

 Properties

 Operators

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Archived Tutorials

 References

 Full Version in PDF/ePUB